static COUNT initialize_flak (ELEMENT *ShipPtr, HELEMENT MissileArray[]) { COUNT i; STARSHIP *StarShipPtr; MISSILE_BLOCK MissileBlock; GetElementStarShip (ShipPtr, &StarShipPtr); MissileBlock.cx = ShipPtr->next.location.x; MissileBlock.cy = ShipPtr->next.location.y; MissileBlock.farray = StarShipPtr->RaceDescPtr->ship_data.weapon; MissileBlock.face = MissileBlock.index = StarShipPtr->ShipFacing; MissileBlock.sender = ShipPtr->playerNr; MissileBlock.flags = IGNORE_SIMILAR; MissileBlock.pixoffs = SPATHI_FORWARD_OFFSET; MissileBlock.speed = (MISSILE_SPEED << RESOLUTION_FACTOR); MissileBlock.hit_points = MISSILE_HITS; MissileBlock.damage = MISSILE_DAMAGE; MissileBlock.life = MISSILE_LIFE; MissileBlock.preprocess_func = flak_preprocess; MissileBlock.blast_offs = MISSILE_OFFSET; for(i = 0; i < 3; ++i) { if (i == 0) { MissileBlock.cx = ShipPtr->next.location.x; MissileBlock.cy = ShipPtr->next.location.y; } else if (i == 1) { MissileBlock.cx = ShipPtr->next.location.x + COSINE(FACING_TO_ANGLE(StarShipPtr->ShipFacing + 4), 12); MissileBlock.cy = ShipPtr->next.location.y + SINE(FACING_TO_ANGLE(StarShipPtr->ShipFacing + 4), 12); } else if (i == 2) { MissileBlock.cx = ShipPtr->next.location.x + COSINE(FACING_TO_ANGLE(StarShipPtr->ShipFacing + 4), -12); MissileBlock.cy = ShipPtr->next.location.y + SINE(FACING_TO_ANGLE(StarShipPtr->ShipFacing + 4), -12); } if ((MissileArray[i] = initialize_missile (&MissileBlock))) { SIZE dx, dy, angle, speed; ELEMENT *MissilePtr; LockElement (MissileArray[i], &MissilePtr); if (i > 0) { angle = GetVelocityTravelAngle (&MissilePtr->velocity); GetCurrentVelocityComponents(&MissilePtr->velocity, &dx, &dy); speed = square_root (dx*dx + dy*dy); if (i == 1) angle += 1; else if (i == 2) angle -= 1; SetVelocityComponents(&MissilePtr->velocity, COSINE(angle, speed), SINE(angle, speed)); } GetCurrentVelocityComponents (&ShipPtr->velocity, &dx, &dy); // Add the Eluder's velocity to its projectiles. DeltaVelocityComponents (&MissilePtr->velocity, dx, dy); MissilePtr->current.location.x -= VELOCITY_TO_WORLD (dx); MissilePtr->current.location.y -= VELOCITY_TO_WORLD (dy); MissilePtr->turn_wait = 1; UnlockElement (MissileArray[i]); } } return (3); }
int main () { float x = .0f; while ( std::cin >> x ) { std::cout << std::setprecision(15) << square_root( x ) << "\n"; } }
int ACE_Stats::std_dev (ACE_Stats_Value &std_dev, const ACE_UINT32 scale_factor) { if (number_of_samples_ <= 1) { std_dev.whole (0); std_dev.fractional (0); } else { const ACE_UINT32 field = std_dev.fractional_field (); // The sample standard deviation is: // // sqrt (sum (sample_i - mean)^2 / (number_of_samples_ - 1)) ACE_UINT64 mean_scaled; // Calculate the mean, scaled, so that we don't lose its // precision. ACE_Stats_Value avg (std_dev.precision ()); mean (avg, 1u); avg.scaled_value (mean_scaled); // Calculate the summation term, of squared differences from the // mean. ACE_UINT64 sum_of_squares = 0; ACE_Unbounded_Queue_Iterator<ACE_INT32> i (samples_); while (! i.done ()) { ACE_INT32 *sample; if (i.next (sample)) { const ACE_UINT64 original_sum_of_squares = sum_of_squares; // Scale up by field width so that we don't lose the // precision of the mean. Carefully . . . const ACE_UINT64 product (*sample * field); ACE_UINT64 difference; // NOTE: please do not reformat this code! It // // works with the Diab compiler the way it is! // if (product >= mean_scaled) // { // difference = product - mean_scaled; // } // else // { // difference = mean_scaled - product; // } // // NOTE: please do not reformat this code! It // // works with the Diab compiler the way it is! // // Square using 64-bit arithmetic. sum_of_squares += difference * ACE_U64_TO_U32 (difference); i.advance (); if (sum_of_squares < original_sum_of_squares) { overflow_ = ENOSPC; return -1; } } } // Divide the summation by (number_of_samples_ - 1), to get the // variance. In addition, scale the variance down to undo the // mean scaling above. Otherwise, it can get too big. ACE_Stats_Value variance (std_dev.precision ()); quotient (sum_of_squares, (number_of_samples_ - 1) * field * field, variance); // Take the square root of the variance to get the standard // deviation. First, scale up . . . ACE_UINT64 scaled_variance; variance.scaled_value (scaled_variance); // And scale up, once more, because we'll be taking the square // root. scaled_variance *= field; ACE_Stats_Value unscaled_standard_deviation (std_dev.precision ()); square_root (scaled_variance, unscaled_standard_deviation); // Unscale. quotient (unscaled_standard_deviation, scale_factor * field, std_dev); } return 0; }
// XXX: This function should be split into two static void self_destruct (ELEMENT *ElementPtr) { STARSHIP *StarShipPtr; GetElementStarShip (ElementPtr, &StarShipPtr); if (ElementPtr->state_flags & PLAYER_SHIP) { HELEMENT hDestruct; // Spawn a temporary element, which dies in this same frame, in order // to defer the effects of the glory explosion. // It will be the last element (or one of the last) for which the // death_func will be called from PostProcessQueue() in this frame. hDestruct = AllocElement (); if (hDestruct) { ELEMENT *DestructPtr; LockElement (hDestruct, &DestructPtr); DestructPtr->playerNr = ElementPtr->playerNr; DestructPtr->state_flags = APPEARING | NONSOLID | FINITE_LIFE; DestructPtr->next.location = ElementPtr->next.location; DestructPtr->life_span = 0; DestructPtr->pParent = ElementPtr->pParent; DestructPtr->hTarget = 0; DestructPtr->death_func = self_destruct; UnlockElement (hDestruct); PutElement (hDestruct); } ElementPtr->state_flags |= NONSOLID; // The ship is now dead. It's death_func, i.e. ship_death(), will be // called the next frame. ElementPtr->life_span = 0; ElementPtr->preprocess_func = destruct_preprocess; } else { // This is called during PostProcessQueue(), close to or at the end, // for the temporary destruct element to apply the effects of glory // explosion. The effects are not seen until the next frame. HELEMENT hElement, hNextElement; for (hElement = GetHeadElement (); hElement != 0; hElement = hNextElement) { ELEMENT *ObjPtr; LockElement (hElement, &ObjPtr); hNextElement = GetSuccElement (ObjPtr); if (CollidingElement (ObjPtr) || ORZ_MARINE (ObjPtr)) { #define DESTRUCT_RANGE 180 SIZE delta_x, delta_y; DWORD dist; if ((delta_x = ObjPtr->next.location.x - ElementPtr->next.location.x) < 0) delta_x = -delta_x; if ((delta_y = ObjPtr->next.location.y - ElementPtr->next.location.y) < 0) delta_y = -delta_y; delta_x = WORLD_TO_DISPLAY (delta_x); delta_y = WORLD_TO_DISPLAY (delta_y); if (delta_x <= DESTRUCT_RANGE && delta_y <= DESTRUCT_RANGE && (dist = (DWORD)(delta_x * delta_x) + (DWORD)(delta_y * delta_y)) <= (DWORD)(DESTRUCT_RANGE * DESTRUCT_RANGE)) { #define MAX_DESTRUCTION (DESTRUCT_RANGE / 10) SIZE destruction; destruction = ((MAX_DESTRUCTION * (DESTRUCT_RANGE - square_root (dist))) / DESTRUCT_RANGE) + 1; if (ObjPtr->state_flags & PLAYER_SHIP) { if (!DeltaCrew (ObjPtr, -destruction)) ObjPtr->life_span = 0; } else if (!GRAVITY_MASS (ObjPtr->mass_points)) { if ((BYTE)destruction < ObjPtr->hit_points) ObjPtr->hit_points -= (BYTE)destruction; else { ObjPtr->hit_points = 0; ObjPtr->life_span = 0; } } } } UnlockElement (hElement); } } }
double my_sqrt(double x) { return square_root(1, x); };
static void spawn_crew (PELEMENT ElementPtr) { if (ElementPtr->state_flags & PLAYER_SHIP) { HELEMENT hCrew; hCrew = AllocElement (); if (hCrew != 0) { ELEMENTPTR CrewPtr; LockElement (hCrew, &CrewPtr); CrewPtr->next.location = ElementPtr->next.location; CrewPtr->state_flags = APPEARING | NONSOLID | FINITE_LIFE | (ElementPtr->state_flags & (GOOD_GUY | BAD_GUY)); CrewPtr->life_span = 0; CrewPtr->death_func = spawn_crew; CrewPtr->pParent = ElementPtr->pParent; CrewPtr->hTarget = 0; UnlockElement (hCrew); PutElement (hCrew); } } else { HELEMENT hElement, hNextElement; for (hElement = GetHeadElement (); hElement != 0; hElement = hNextElement) { ELEMENTPTR ObjPtr; LockElement (hElement, &ObjPtr); hNextElement = GetSuccElement (ObjPtr); if ((ObjPtr->state_flags & PLAYER_SHIP) && (ObjPtr->state_flags & (GOOD_GUY | BAD_GUY)) != (ElementPtr->state_flags & (GOOD_GUY | BAD_GUY)) && ObjPtr->crew_level > 1) { SIZE dx, dy; DWORD d_squared; dx = ObjPtr->next.location.x - ElementPtr->next.location.x; if (dx < 0) dx = -dx; dy = ObjPtr->next.location.y - ElementPtr->next.location.y; if (dy < 0) dy = -dy; dx = WORLD_TO_DISPLAY (dx); dy = WORLD_TO_DISPLAY (dy); #define ABANDONER_RANGE 208 /* originally SPACE_HEIGHT */ if (dx <= ABANDONER_RANGE && dy <= ABANDONER_RANGE && (d_squared = (DWORD)((UWORD)dx * (UWORD)dx) + (DWORD)((UWORD)dy * (UWORD)dy)) <= (DWORD)((UWORD)ABANDONER_RANGE * (UWORD)ABANDONER_RANGE)) { #define MAX_ABANDONERS 8 COUNT crew_loss; crew_loss = ((MAX_ABANDONERS * (ABANDONER_RANGE - square_root (d_squared))) / ABANDONER_RANGE) + 1; if (crew_loss >= ObjPtr->crew_level) crew_loss = ObjPtr->crew_level - 1; AbandonShip (ObjPtr, ElementPtr, crew_loss); } } UnlockElement (hElement); } } }
/** * \return a Gaussian sample of the type \c Variate determined by mapping a * noise sample into the Gaussian sample space * * \param sample Noise Sample * * \throws see square_root() */ Variate map_standard_normal(const StandardVariate& sample) const override { return mean() + square_root() * sample; }
//Function to calculate the color contribution of each ray color_fixed lightContribution ( node* currNode, scene *myScene ) { color_fixed c1 = { 0, 0, 0 }; coord t = currNode->t; triangle* intersection = currNode->lastIntercepted; if ( intersection == NULL ) { //printf( "no intersection \n" ); return c1; } point_fixed intersectionPoint = currNode->v.start + t * currNode->v.dir; vect_fixed normal = normalTriangle( intersection, myScene ); material intersectMat = myScene->materials[ intersection->materialId ]; for( unsigned int j = 0; j < myScene->NbLights; ++j ) { coord n1, n2; light currentl = myScene->lights[j]; // vector length from the intersection point to the l vect_fixed dist = currentl.pos - intersectionPoint; n1 = currNode->v.dir * normal; //root->v.dir.x * normal.x + root->v.dir.y * normal.y + root->v.dir.z // * normal.z ; if (n1 > 0) { normal.x = -normal.x; normal.y = -normal.y; normal.z = -normal.z; } n1 = currNode->v.dir * normal; n2 = dist * normal; if( n1 < 0 && n2 > 0 ) { //now let's create a ray from the intersection point to //the light source ray lightRay; lightRay.start = intersectionPoint; lightRay.dir = ( 1 / square_root( dist * dist ) ) * dist; //let's see if there are objects in the middle of the road that can // cause reflection or shadow // light ray and ray have to be in the same direction so proyection // from the light ray in the ray has to have the // same direction so cosI positive int shadow = 0; kdTreeNode* shadowPack = currNode->currentPacket; kdTreeNode* newShadowPack = NULL; //incrementing the ray starting point for avoiding self intersection //printf( "shadow verification process\n" ); while( 1 ) { TrigRefList* currTrigRef = shadowPack->trig; while( currTrigRef != NULL ) { coord u = 0; if ( hitTriangle( &lightRay, currTrigRef->ref, &u ) == 1 ) { if( u == 0 ) { //not self intersection in the same packet currTrigRef = currTrigRef->next; continue; } if ( currTrigRef->ref != currNode->lastIntercepted ) { //not self intersection in different packets //is it really necessary ? printf( "Intersection found - Trig Index: %d" \ "- shadow region for light %d\n\n ", currTrigRef->ref->index, j ); cout << "shadow ray start x: " << lightRay.start.x << " y: " << lightRay.start.y << " z: " << lightRay.start.z << endl; cout << "shadow ray dir x : " << lightRay.dir.x << " y: " << lightRay.dir.y << " z: " << lightRay.dir.z << endl; cout << "u: " << u; shadow = 1; break; } else { printf( "yes! It is necessary\n " ); //exit(0); } } currTrigRef = currTrigRef->next; } if( shadow == 1 ) { break; //shadow found } else { //printf( "getting next packet\n" ); newShadowPack = getTrianguleList( &lightRay, shadowPack, myScene->tree ); if( newShadowPack != NULL && newShadowPack != shadowPack ) { currTrigRef = newShadowPack->trig; // for( int i = 0; i < newShadowPack->pnum; i++ ) // { // cout << "index: " << currTrigRef->ref->index // << endl; // cout << "v1 - x: " << currTrigRef->ref->V1.x // << " y: " << currTrigRef->ref->V1.y << " z: " // << currTrigRef->ref->V1.z << endl; // cout << "v2 - x: " << currTrigRef->ref->V2.x // << " y: " << currTrigRef->ref->V2.y // << " z: " << currTrigRef->ref->V2.z << endl; // cout << "v3 - x: " << currTrigRef->ref->V3.x // << " y: " << currTrigRef->ref->V3.y // << " z: " << currTrigRef->ref->V3.z << endl; // cout << endl; // currTrigRef = currTrigRef->next; // } } else { //cout << "no more triangles" << endl; break; } shadowPack = newShadowPack; } } //we considered the objects as lambert-surface ones so this means // that the Intensity from the incoming ray // depends on the angle between the normal //if there's no shadow, we considered the current light source // effect : Io=In*cos(0); if ( shadow == 0 ) { mfp coef=1; for(unsigned int q=0; q<= (unsigned int)(currNode->level); ++q) { coef*=intersectMat.reflection; } if ((currNode->type==REFLECTED_RAY) || (currNode->type==ROOT_RAY) || (currNode->type==TOTAL_REFLECTED_RAY)) { // lambert // cout << " coef: " << coef // << " level: " << root->level << endl; // intensity from objects material mfp lambert = (lightRay.dir * normal) * coef; // cout << " lambert: " << lambert << endl; c1.red = c1.red + lambert * currentl.red * intersectMat.red; c1.green = (c1.green + lambert * currentl.green * intersectMat.green); c1.blue = (c1.blue + lambert * currentl.blue * intersectMat.blue); // cout << "color r2 R " << c1.red // << " g " << c1.green // << " b " << c1.blue << endl; } else if (currNode->type==REFRACTED_RAY) { //pseudo beer c1.red = c1.red + coef * intersectMat.red; c1.green = c1.green + coef * intersectMat.green; c1.blue = c1.blue + coef * intersectMat.blue; } } } else { //cout << "FFFUUUU" << endl; } } return c1; }