void yarrAI :: avoid_ships() { /// Look around and find the ship closest to this one in its vision /// /// Returns a ship pointer on success, /// returns a NULL POINTER on failure SPfloat closest_dist = feel_dist + 1; // higher than vision to ensure we see things spaceShip* closest_ship = NULL; vector<spaceShip*>::iterator ships_i; for (ships_i = puppet->sector->ships.begin() ; ships_i != puppet->sector->ships.end() ; ships_i++) { // NOTE : this includes allies if( canfeel((*ships_i)->pos) ) { if( puppet->pos.dist( (*ships_i)->pos ) < closest_dist) closest_ship = (*ships_i); } } if( closest_ship != NULL) { avoid(closest_ship->pos); thrust(); } }
void Seguidor::runing_Seguidor(){ //communication_principal();//----------------------------------------------------------------OJO if(robot_active){ Serial.println("Activo"); if(!infrarred_active) line_position = desired_position; distance_sensor = 0; if(distance_active) if(digitalRead(distance_sensor_pin) == HIGH){ distance_sensor = 1; }else{ avoid(); } difference_time = actual_time - encoders_time; if(difference_time >= bypass_time_encoders){ this->calculate_angular_values(); encoders_time = actual_time; } if(!in_obstaculo){ PID_processing(); adjust_velocities(); if(actual_time - summatory_time >= 500){ if(summatory_PWMM1 > summatory_PWMM2) ultima_curva = 0;//Serial1.println("message/--------------------------------------------------Curva derecha"); else if(summatory_PWMM1 < summatory_PWMM2) ultima_curva = 1; summatory_PWMM1 = summatory_PWMM2 = 0; summatory_time = actual_time; }else{ summatory_PWMM1 += pwmM1; summatory_PWMM2 += pwmM2; } }else{ if(comprobar_Sensores_obstaculo()){ in_obstaculo = false; summatory_time = millis(); } } }else{ change_Velocity(0,0); change_Direction(0,0); } actual_time = millis(); loop_time = actual_time - before_time; before_time = actual_time; }
void starbase_attack_warning(struct mtwist_state *mt, char *buffer, int buflen, int line_len) { char do_avoid[100]; strcpy(do_avoid, avoid(mt)); do_avoid[0] = toupper(do_avoid[0]); snprintf(buffer, buflen, "%s %s and %s or %s %s\n", be_advised(mt), cease_fire(mt), get_lost(mt), you_will_be(mt), destroyed(mt)); break_lines(buffer, line_len); }
bool KDialog::avoidArea( QWidget *w, const QRect& area, int screen ) { if ( !w ) return false; QRect fg = w->frameGeometry(); if ( !fg.intersects( area ) ) return true; // nothing to do. QRect scr = screenRect( w, screen ); QRect avoid( area ); // let's add some margin avoid.moveBy( -5, -5 ); avoid.rRight() += 10; avoid.rBottom() += 10; if ( QMAX( fg.top(), avoid.top() ) <= QMIN( fg.bottom(), avoid.bottom() ) ) { // We need to move the widget up or down int spaceAbove = QMAX(0, avoid.top() - scr.top()); int spaceBelow = QMAX(0, scr.bottom() - avoid.bottom()); if ( spaceAbove > spaceBelow ) // where's the biggest side? if ( fg.height() <= spaceAbove ) // big enough? fg.setY( avoid.top() - fg.height() ); else return false; else if ( fg.height() <= spaceBelow ) // big enough? fg.setY( avoid.bottom() ); else return false; } if ( QMAX( fg.left(), avoid.left() ) <= QMIN( fg.right(), avoid.right() ) ) { // We need to move the widget left or right int spaceLeft = QMAX(0, avoid.left() - scr.left()); int spaceRight = QMAX(0, scr.right() - avoid.right()); if ( spaceLeft > spaceRight ) // where's the biggest side? if ( fg.width() <= spaceLeft ) // big enough? fg.setX( avoid.left() - fg.width() ); else return false; else if ( fg.width() <= spaceRight ) // big enough? fg.setX( avoid.right() ); else return false; } //kdDebug() << "Moving window to " << fg.x() << "," << fg.y() << endl; w->move(fg.x(), fg.y()); return true; }
void update() { bool updating = lastmillis-updatemillis > 100; // fixed rate logic at 10fps loopv(players) if(players[i]->ai) { if(updating && updatemillis < lastmillis) { avoid(); forcegun = multiplayer(false) ? -1 : aiforcegun; updatemillis = lastmillis; } if(!intermission) think(players[i], updating); else players[i]->stopmoving(); } }
void planet_description(struct mtwist_state *mt, char *buffer, int buflen, int line_len) { char do_avoid[100]; strcpy(do_avoid, avoid(mt)); do_avoid[0] = toupper(do_avoid[0]); snprintf(buffer, buflen, "This %s %s %s %s %s %s %s and %s %s %s %s. %s the %s %s. %s %s.\n", climate(mt), planet(mt), known_for(mt), producing(mt), exceptional(mt), qnationality(mt), product(mt), known_for(mt), exceptional(mt), qnationality(mt), culture(mt), do_avoid, terrible(mt), product(mt), bring_your(mt), traveling_accessory(mt)); break_lines(buffer, line_len); }
void yarrAI :: avoid_bullets() { /// Look around and find the ship closest to this one in its vision /// /// Returns a ship pointer on success, /// returns a NULL POINTER on failure SPfloat closest_dist = feel_dist + 1; // higher than vision to ensure we see things spaceBullet* closest_bullet = NULL; vector<spaceWeapon*>::iterator weapons_i; for (weapons_i = puppet->sector->weapons.begin() ; weapons_i != puppet->sector->weapons.end() ; weapons_i++) { vector<spaceBullet>::iterator bullet_i; for(bullet_i = (*weapons_i)->part.begin() ; bullet_i != (*weapons_i)->part.end() ; bullet_i++) { //if( (*bullets_i)->alive == true ) // don't collide with parent ship or allies if( bullet_i->team_id != puppet->team_id ) if( canfeel(bullet_i->pos)) if( puppet->pos.dist( bullet_i->pos ) < closest_dist) closest_bullet = bullet_i; } } if( closest_bullet != NULL) { avoid(closest_bullet->pos); thrust(); } }
//----------------------------------------------------------------------------- // Client-side obstacle avoidance //----------------------------------------------------------------------------- void C_BaseHLPlayer::PerformClientSideObstacleAvoidance( float flFrameTime, CUserCmd *pCmd ) { // Don't avoid if noclipping or in movetype none switch ( GetMoveType() ) { case MOVETYPE_NOCLIP: case MOVETYPE_NONE: case MOVETYPE_OBSERVER: return; default: break; } // Try to steer away from any objects/players we might interpenetrate Vector size = WorldAlignSize(); float radius = 0.7f * sqrt( size.x * size.x + size.y * size.y ); float curspeed = GetLocalVelocity().Length2D(); //int slot = 1; //engine->Con_NPrintf( slot++, "speed %f\n", curspeed ); //engine->Con_NPrintf( slot++, "radius %f\n", radius ); // If running, use a larger radius float factor = 1.0f; if ( curspeed > 150.0f ) { curspeed = MIN( 2048.0f, curspeed ); factor = ( 1.0f + ( curspeed - 150.0f ) / 150.0f ); //engine->Con_NPrintf( slot++, "scaleup (%f) to radius %f\n", factor, radius * factor ); radius = radius * factor; } Vector currentdir; Vector rightdir; QAngle vAngles = pCmd->viewangles; vAngles.x = 0; AngleVectors( vAngles, ¤tdir, &rightdir, NULL ); bool istryingtomove = false; bool ismovingforward = false; if ( fabs( pCmd->forwardmove ) > 0.0f || fabs( pCmd->sidemove ) > 0.0f ) { istryingtomove = true; if ( pCmd->forwardmove > 1.0f ) { ismovingforward = true; } } if ( istryingtomove == true ) radius *= 1.3f; CPlayerAndObjectEnumerator avoid( radius ); partition->EnumerateElementsInSphere( PARTITION_CLIENT_SOLID_EDICTS, GetAbsOrigin(), radius, false, &avoid ); // Okay, decide how to avoid if there's anything close by int c = avoid.GetObjectCount(); if ( c <= 0 ) return; //engine->Con_NPrintf( slot++, "moving %s forward %s\n", istryingtomove ? "true" : "false", ismovingforward ? "true" : "false" ); float adjustforwardmove = 0.0f; float adjustsidemove = 0.0f; for ( int i = 0; i < c; i++ ) { C_AI_BaseNPC *obj = dynamic_cast< C_AI_BaseNPC *>(avoid.GetObject( i )); if( !obj ) continue; Vector vecToObject = obj->GetAbsOrigin() - GetAbsOrigin(); float flDist = vecToObject.Length2D(); // Figure out a 2D radius for the object Vector vecWorldMins, vecWorldMaxs; obj->CollisionProp()->WorldSpaceAABB( &vecWorldMins, &vecWorldMaxs ); Vector objSize = vecWorldMaxs - vecWorldMins; float objectradius = 0.5f * sqrt( objSize.x * objSize.x + objSize.y * objSize.y ); //Don't run this code if the NPC is not moving UNLESS we are in stuck inside of them. if ( !obj->IsMoving() && flDist > objectradius ) continue; if ( flDist > objectradius && obj->IsEffectActive( EF_NODRAW ) ) { obj->RemoveEffects( EF_NODRAW ); } Vector vecNPCVelocity; obj->EstimateAbsVelocity( vecNPCVelocity ); float flNPCSpeed = VectorNormalize( vecNPCVelocity ); Vector vPlayerVel = GetAbsVelocity(); VectorNormalize( vPlayerVel ); float flHit1, flHit2; Vector vRayDir = vecToObject; VectorNormalize( vRayDir ); float flVelProduct = DotProduct( vecNPCVelocity, vPlayerVel ); float flDirProduct = DotProduct( vRayDir, vPlayerVel ); if ( !IntersectInfiniteRayWithSphere( GetAbsOrigin(), vRayDir, obj->GetAbsOrigin(), radius, &flHit1, &flHit2 ) ) continue; Vector dirToObject = -vecToObject; VectorNormalize( dirToObject ); float fwd = 0; float rt = 0; float sidescale = 2.0f; float forwardscale = 1.0f; bool foundResult = false; Vector vMoveDir = vecNPCVelocity; if ( flNPCSpeed > 0.001f ) { // This NPC is moving. First try deflecting the player left or right relative to the NPC's velocity. // Start with whatever side they're on relative to the NPC's velocity. Vector vecNPCTrajectoryRight = CrossProduct( vecNPCVelocity, Vector( 0, 0, 1) ); int iDirection = ( vecNPCTrajectoryRight.Dot( dirToObject ) > 0 ) ? 1 : -1; for ( int nTries = 0; nTries < 2; nTries++ ) { Vector vecTryMove = vecNPCTrajectoryRight * iDirection; VectorNormalize( vecTryMove ); Vector vTestPosition = GetAbsOrigin() + vecTryMove * radius * 2; if ( TestMove( vTestPosition, size.z * 2, radius * 2, obj->GetAbsOrigin(), vMoveDir ) ) { fwd = currentdir.Dot( vecTryMove ); rt = rightdir.Dot( vecTryMove ); //Msg( "PUSH DEFLECT fwd=%f, rt=%f\n", fwd, rt ); foundResult = true; break; } else { // Try the other direction. iDirection *= -1; } } } else { // the object isn't moving, so try moving opposite the way it's facing Vector vecNPCForward; obj->GetVectors( &vecNPCForward, NULL, NULL ); Vector vTestPosition = GetAbsOrigin() - vecNPCForward * radius * 2; if ( TestMove( vTestPosition, size.z * 2, radius * 2, obj->GetAbsOrigin(), vMoveDir ) ) { fwd = currentdir.Dot( -vecNPCForward ); rt = rightdir.Dot( -vecNPCForward ); if ( flDist < objectradius ) { obj->AddEffects( EF_NODRAW ); } //Msg( "PUSH AWAY FACE fwd=%f, rt=%f\n", fwd, rt ); foundResult = true; } } if ( !foundResult ) { // test if we can move in the direction the object is moving Vector vTestPosition = GetAbsOrigin() + vMoveDir * radius * 2; if ( TestMove( vTestPosition, size.z * 2, radius * 2, obj->GetAbsOrigin(), vMoveDir ) ) { fwd = currentdir.Dot( vMoveDir ); rt = rightdir.Dot( vMoveDir ); if ( flDist < objectradius ) { obj->AddEffects( EF_NODRAW ); } //Msg( "PUSH ALONG fwd=%f, rt=%f\n", fwd, rt ); foundResult = true; } else { // try moving directly away from the object Vector vTestPosition = GetAbsOrigin() - dirToObject * radius * 2; if ( TestMove( vTestPosition, size.z * 2, radius * 2, obj->GetAbsOrigin(), vMoveDir ) ) { fwd = currentdir.Dot( -dirToObject ); rt = rightdir.Dot( -dirToObject ); foundResult = true; //Msg( "PUSH AWAY fwd=%f, rt=%f\n", fwd, rt ); } } } if ( !foundResult ) { // test if we can move through the object Vector vTestPosition = GetAbsOrigin() - vMoveDir * radius * 2; fwd = currentdir.Dot( -vMoveDir ); rt = rightdir.Dot( -vMoveDir ); if ( flDist < objectradius ) { obj->AddEffects( EF_NODRAW ); } //Msg( "PUSH THROUGH fwd=%f, rt=%f\n", fwd, rt ); foundResult = true; } // If running, then do a lot more sideways veer since we're not going to do anything to // forward velocity if ( istryingtomove ) { sidescale = 6.0f; } if ( flVelProduct > 0.0f && flDirProduct > 0.0f ) { sidescale = 0.1f; } float force = 1.0f; float forward = forwardscale * fwd * force * AVOID_SPEED; float side = sidescale * rt * force * AVOID_SPEED; adjustforwardmove += forward; adjustsidemove += side; } pCmd->forwardmove += adjustforwardmove; pCmd->sidemove += adjustsidemove; // Clamp the move to within legal limits, preserving direction. This is a little // complicated because we have different limits for forward, back, and side //Msg( "PRECLAMP: forwardmove=%f, sidemove=%f\n", pCmd->forwardmove, pCmd->sidemove ); float flForwardScale = 1.0f; if ( pCmd->forwardmove > fabs( cl_forwardspeed.GetFloat() ) ) { flForwardScale = fabs( cl_forwardspeed.GetFloat() ) / pCmd->forwardmove; } else if ( pCmd->forwardmove < -fabs( cl_backspeed.GetFloat() ) ) { flForwardScale = fabs( cl_backspeed.GetFloat() ) / fabs( pCmd->forwardmove ); } float flSideScale = 1.0f; if ( fabs( pCmd->sidemove ) > fabs( cl_sidespeed.GetFloat() ) ) { flSideScale = fabs( cl_sidespeed.GetFloat() ) / fabs( pCmd->sidemove ); } float flScale = MIN( flForwardScale, flSideScale ); pCmd->forwardmove *= flScale; pCmd->sidemove *= flScale; //Msg( "POSTCLAMP: forwardmove=%f, sidemove=%f\n", pCmd->forwardmove, pCmd->sidemove ); }
inline void SchoolFish::applyAvoidance(Vector &accumulator) { // only avoid the player if not in the background if (this->layer < LR_ELEMENTS10) { if ((dsq->game->avatar->position - this->position).isLength2DIn(128)) { avoid(accumulator, dsq->game->avatar->position); } } //return; if (avoidTime>0) return; VectorSet closestObs; VectorSet obsPos; //Vector closestObs; int range = 10; int radius = range*TILE_SIZE; Vector p; TileVector t0(position); TileVector t; for (int x = -range; x <= range; x++) { for (int y = -range; y <= range; y++) { TileVector t = t0; t.x+=x; t.y+=y; if (dsq->game->isObstructed(t)) { p = t.worldVector(); closestObs.push_back(this->position - p); obsPos.push_back(p); /* std::ostringstream os; os << "tile(" << t.x << ", " << t.y << ") p(" << p.x << ", " << p.y << ")"; debugLog(os.str()); */ /* int len = (p - this->position).getSquaredLength2D(); if (len < sqr(radius)) { closestObs.push_back(this->position - p); obsPos.push_back(p); } */ } } } if (!closestObs.empty()) { //avoid (accumulator, this->averageVectors(closestObs)); //accumulator = Vector(0,0,0); Vector change; change = averageVectors(closestObs); //change |= 200; float dist = (this->position - averageVectors(obsPos)).getLength2D(); float ratio = dist / radius; if (ratio < minUrgency) ratio = minUrgency; else if (ratio > maxUrgency) ratio = maxUrgency; change.setLength2D(ratio + lastVel.getLength2D()/10); accumulator += change; } if (this->range!=0) { if (!((position - startPos).isLength2DIn(this->range))) { Vector diff = startPos - position; diff.setLength2D(lastVel.getLength2D()); accumulator += diff; } } }
genericinit () { expavoidval = avoid(); return (1); }
task main() { startTask(readSensors); avoid(); }
inline void SchoolFish::applyAvoidance(Vector &accumulator) { // only avoid the player if not in the background if (this->layer < LR_ELEMENTS10) { if ((dsq->game->avatar->position - this->position).isLength2DIn(128)) { avoid(accumulator, dsq->game->avatar->position); } } //return; if (avoidTime>0) return; const int range = 10; const int step = 1; int radius = range*TILE_SIZE; int obsSumX = 0, obsSumY = 0; // Not a Vector (avoid using floats) int obsCount = 0; const TileVector t0(position); TileVector t; for (t.x = t0.x-range; t.x <= t0.x+range; t.x += step) { for (t.y = t0.y-range; t.y <= t0.y+range; t.y += step) { if (dsq->game->isObstructed(t)) { obsSumX += t0.x - t.x; obsSumY += t0.y - t.y; obsCount++; } } } if (obsCount > 0) { const float tileMult = (float)TILE_SIZE / (float)obsCount; Vector change(obsSumX*tileMult, obsSumY*tileMult); change += position - t0.worldVector(); //change |= 200; float dist = change.getLength2D(); float ratio = dist / radius; if (ratio < minUrgency) ratio = minUrgency; else if (ratio > maxUrgency) ratio = maxUrgency; change *= (ratio + lastSpeed*0.1f) / dist; accumulator += change; } if (this->range!=0) { if (!((position - startPos).isLength2DIn(this->range))) { Vector diff = startPos - position; diff.setLength2D(lastSpeed); accumulator += diff; } } }