Beispiel #1
0
/* ----------------------------------------------------------------------------
 * Emits the particles, regardless of the timer.
 * manager: The particle manager to place these particles on.
 */
void particle_generator::emit(particle_manager &manager) {
    size_t final_nr =
        max(
            0,
            (int) number +
            randomi((int) (0 - number_deviation), (int) number_deviation)
        );
        
    for(size_t p = 0; p < final_nr; ++p) {
        particle new_p = base_particle;
        
        new_p.duration =
            max(
                0.0f,
                new_p.duration +
                randomf(-duration_deviation, duration_deviation)
            );
        new_p.time = new_p.duration;
        new_p.friction +=
            randomf(-friction_deviation, friction_deviation);
        new_p.gravity +=
            randomf(-gravity_deviation, gravity_deviation);
        new_p.x +=
            randomf(-x_deviation, x_deviation);
        new_p.y +=
            randomf(-y_deviation, y_deviation);
        new_p.size =
            max(
                0.0f,
                new_p.size +
                randomf(-size_deviation, size_deviation)
            );
        //For speed, let's decide if we should use
        //(speed_x and speed_y) or (speed and angle).
        //We'll use whichever one is not all zeros.
        if(angle != 0 || speed != 0) {
            angle_to_coordinates(
                angle + randomf(-angle_deviation, angle_deviation),
                speed + randomf(-speed_deviation, speed_deviation),
                &new_p.speed_x, &new_p.speed_y
            );
        } else {
            new_p.speed_x +=
                randomf(-speed_x_deviation, speed_x_deviation);
            new_p.speed_y +=
                randomf(-speed_y_deviation, speed_y_deviation);
        }
        
        manager.add(new_p);
    }
}
int main ()   
{   
   long i;  long Ncirc = 0;   
   double pi, x, y, test;   
   double r = 1.0;   // radius of circle. Side of squrare is 2*r    
   
   seed(-r, r);  // The circle and square are centered at the origin   
   
   for(i=0;i<num_trials; i++)   
   {   
      x = randomi();    
      y = randomi();   
   
      test = x*x + y*y;   
   
      if (test <= r*r) Ncirc++;   
    }   
   
    pi = 4.0 * ((double)Ncirc/(double)num_trials);   
   
    printf("\n %ld trials, pi is %f \n",num_trials, pi);   
   
    return 0;   
}   
Beispiel #3
0
/* ----------------------------------------------------------------------------
 * Checks if the attack should miss, and returns the result.
 * If it was already decided that it missed in a previous frame, that's a
 * straight no. If not, it will roll with the hit rate to check.
 * If the attack is a miss, it also registers the miss, so that we can keep
 * memory of it for the next frames.
 */
bool pikmin::process_attack_miss(hitbox_interaction* info) {
    if(info->mob2->anim.cur_anim == missed_attack_ptr) {
        //In a previous frame, we had already considered this animation a miss.
        return false;
    }
    
    unsigned char hit_rate = info->mob2->anim.cur_anim->hit_rate;
    if(hit_rate == 0) return false;
    
    unsigned char hit_roll = randomi(0, 100);
    if(hit_roll > hit_rate) {
        //This attack was randomly decided to be a miss.
        //Record this animation so it won't be considered a hit next frame.
        missed_attack_ptr = info->mob2->anim.cur_anim;
        missed_attack_timer.start();
        return false;
    }
    
    return true;
}
/* ----------------------------------------------------------------------------
 * Makes a mob move to a spot because it's being carried.
 * m:  Mob to start moving (the treasure, for instance).
 * np: New Pikmin; the Pikmin that justed joined the carriers. Used to detect ties and tie-breaking.
 * lp: Leaving Pikmin; the Pikmin that just left the carriers. Used to detect ties and tie-breaking.
 */
void start_carrying(mob* m, pikmin* np, pikmin* lp) {
    // TODO what if an Onion hasn't been revelead yet?
    if(!m->carrier_info) return;
    
    if(m->carrier_info->carry_to_ship) {
    
        m->set_target(
            ships[0]->x + ships[0]->type->radius + m->type->radius + 8,
            ships[0]->y,
            NULL,
            NULL,
            false);
        m->carrier_info->decided_type = NULL;
        
    } else {
    
        map<pikmin_type*, unsigned> type_quantity; // How many of each Pikmin type are carrying.
        vector<pikmin_type*> majority_types; // The Pikmin type with the most carriers.
        
        // First, count how many of each type there are carrying.
        for(size_t p = 0; p < m->carrier_info->max_carriers; p++) {
            pikmin* pik_ptr = NULL;
            
            if(m->carrier_info->carrier_spots[p] == NULL) continue;
            if(typeid(*m->carrier_info->carrier_spots[p]) != typeid(pikmin)) continue;
            
            pik_ptr = (pikmin*) m->carrier_info->carrier_spots[p];
            
            if(!pik_ptr->pik_type->has_onion) continue; // If it doesn't have an Onion, it won't even count. // TODO what if it hasn't been discovered / Onion not on this area?
            
            type_quantity[pik_ptr->pik_type]++;
        }
        
        // Then figure out what are the majority types.
        unsigned most = 0;
        for(auto t = type_quantity.begin(); t != type_quantity.end(); t++) {
            if(t->second > most) {
                most = t->second;
                majority_types.clear();
            }
            if(t->second == most) majority_types.push_back(t->first);
        }
        
        // If we ended up with no candidates, pick a type at random, out of all possible types.
        if(majority_types.empty()) {
            for(auto t = pikmin_types.begin(); t != pikmin_types.end(); t++) {
                if(t->second->has_onion) { // TODO what if it hasn't been discovered / Onion not on this area?
                    majority_types.push_back(t->second);
                }
            }
        }
        
        // Now let's pick an Onion.
        if(majority_types.empty()) {
            return; // TODO warn that something went horribly wrong?
            
        } else if(majority_types.size() == 1) {
            // If there's only one possible type to pick, pick it.
            m->carrier_info->decided_type = majority_types[0];
            
        } else {
            // If there's a tie, let's take a careful look.
            bool new_tie = false;
            
            // Is the Pikmin that just joined part of the majority types?
            // If so, that means this Pikmin just created a NEW tie!
            // So let's pick a random Onion again.
            if(np) {
                for(size_t mt = 0; mt < majority_types.size(); mt++) {
                    if(np->type == majority_types[mt]) {
                        new_tie = true;
                        break;
                    }
                }
            }
            
            // If a Pikmin left, check if it is related to the majority types.
            // If not, then a new tie wasn't made, no worries.
            // If it was related, a new tie was created.
            if(lp) {
                new_tie = false;
                for(size_t mt = 0; mt < majority_types.size(); mt++) {
                    if(lp->type == majority_types[mt]) {
                        new_tie = true;
                        break;
                    }
                }
            }
            
            // Check if the previously decided type belongs to one of the majorities.
            // If so, it can be chosen again, but if not, it cannot.
            bool can_continue = false;
            for(size_t mt = 0; mt < majority_types.size(); mt++) {
                if(majority_types[mt] == m->carrier_info->decided_type) {
                    can_continue = true;
                    break;
                }
            }
            if(!can_continue) m->carrier_info->decided_type = NULL;
            
            // If the Pikmin that just joined is not a part of the majorities,
            // then it had no impact on the existing ties.
            // Go with the Onion that had been decided before.
            if(new_tie || !m->carrier_info->decided_type) {
                m->carrier_info->decided_type = majority_types[randomi(0, majority_types.size() - 1)];
            }
        }
        
        
        // Figure out where that type's Onion is.
        size_t onion_nr = 0;
        for(; onion_nr < onions.size(); onion_nr++) {
            if(onions[onion_nr]->oni_type->pik_type == m->carrier_info->decided_type) {
                break;
            }
        }
        
        // Finally, start moving the mob.
        m->set_target(onions[onion_nr]->x, onions[onion_nr]->y, NULL, NULL, false);
        m->set_state(MOB_STATE_BEING_CARRIED);
        sfx_pikmin_carrying.play(-1, true);
    }
}
Beispiel #5
0
/* implementation of Test B */
void testb_check(size_t const n) {
  list_t lists[NUM_OF_LISTS];
  int *added_value;
  int values[NUM_OF_LISTS][n];
  int ret, i, j;
  int value;

  // --------------
  // INITIALIZATION
  // of the lists and check for correctness
  // --------------
  for(i=0; i<NUM_OF_LISTS; ++i) {
    ret = list_init(&lists[i]);
    assert(ret == 0 && "list initialization fails");
  }

  // for achieving the insertion of new elements creating new
  // memory areas, instead of calling malloc(), we set the
  // list_attribute_copy and check the correctness.
  for(i=0; i<NUM_OF_LISTS; ++i) {
    ret = list_attributes_copy(&lists[i], int_size, 1);
    assert(ret == 0 && "setting attribute copy fails");
  }

  // ---------
  // INSERTION
  // both successful and unsuccessful
  // ---------
  for(i=0; i<NUM_OF_LISTS; ++i) {
    // successful insertion in empty list
    randomi(&value);
    ret = list_insert_at(&lists[i], &value, 0);
    assert(ret > 0 && "element insertion fails");
    assert(list_size(&lists[i]) == 1 && "list size is wrong");
    // checking the inserted value
    added_value = (int *)list_get_at(&lists[i], 0);
    assert(added_value != NULL && "retrieving element fails");
    assert(value == *added_value && "retrieved value has wrong value");
    // successful deletion 
    ret = list_delete_at(&lists[i], 0);
    assert(ret == 0 && "delete element fails");
    assert(list_size(&lists[i]) == 0 && "list size is wrong");
  }

  // Failure of insertion on multiple elements.
  // The insertion is done at indexes that are not
  // reachable for the list because it's empty
  for(i=0; i<NUM_OF_LISTS; ++i) {
    randomi(&value);
    for(j=1; j<4; ++j) {
      ret = list_insert_at(&lists[i], &value, j);
      assert(ret < 0 && "element insertion failure fails");
      assert(list_size(&lists[i]) == 0 && "list size is wrong");

      ret = list_delete_at(&lists[i], j);
      assert(ret < 0 && "element deletion failure fails");
      assert(list_size(&lists[i]) == 0 && "list size is wrong");
    }
  }

  // ---------
  // PREAPPEND
  // ---------

  // append the second half of the lists
  for(i=0; i<NUM_OF_LISTS; ++i) {
    for(j=0; j<(n/2); ++j) {
      randomi(&value);
      ret = list_prepend(&lists[i], &value);
      assert(ret == 1 && "element prepend fails");
      // checking also the value appended
      added_value = (int *)list_get_at(&lists[i], 0);
      assert(added_value != NULL && "retrieving element fails");
      assert(value == *added_value && "retrieved value has wrong value");
      // store the max for each list
      values[i][(n/2-1)-j] = value;
    }

    assert(list_size(&lists[i]) == n/2 && "list size is wrong");
  }

  // ------
  // APPEND
  // ------

  // append the first half of the lists
  for(i=0; i<NUM_OF_LISTS; ++i) {
    for(j=(n/2); j<n; ++j) {
      randomi(&value);
      ret = list_append(&lists[i], &value);
      assert(ret == 1 && "element append fails");
      // checking also the value appended
      added_value = (int *)list_get_at(&lists[i], j);
      assert(added_value != NULL && "retrieving element fails");
      assert(value == *added_value && "retrieved value has wrong value");
      // store the value in the matrix
      values[i][j] = value;
    }

    assert(list_size(&lists[i]) == n && "list size is wrong");
  }

  // check the values inserted in the lists
  for(i=0; i<NUM_OF_LISTS; ++i) {
    for(j=0; j<n; ++j) {
      assert(values[i][j] == *(int *)list_get_at(&lists[i], j)
             && "retrieved value has wrong value");
    }
  }

  // -----
  // CLEAR
  // -----

  // check the correctness of the clear function execution
  // and check also the length of the cleared list
  for(i=0; i<NUM_OF_LISTS; ++i) {
      unsigned int isize;
    isize = list_size(&lists[i]);

    ret = list_clear(&lists[i]);
    assert(ret == (int)isize && "clearing list fails");

    ret = list_size(&lists[i]);
    assert(ret == 0 && "list size is wrong");
  }

  // -------
  // DESTROY
  // -------

  // destroy both lists
  for(i=0; i<NUM_OF_LISTS; ++i)
    list_destroy(&lists[i]);
}
Beispiel #6
0
void Grass::fixTexType()
{
	texPart_x = randomi(0,6);
}