X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FAIModel%2FAIGroundVehicle.cxx;h=b813b3c8211f3e3b2cf1d3f0f72366db4a70ec90;hb=afcdbd3158503773644aa07dd86cd92d67946bd4;hp=10ea47e596f2978139f08460a086e914a83ca916;hpb=fdbfd6fd0b789eecf2c532d048a75d15f8d35ad8;p=flightgear.git diff --git a/src/AIModel/AIGroundVehicle.cxx b/src/AIModel/AIGroundVehicle.cxx index 10ea47e59..b813b3c82 100644 --- a/src/AIModel/AIGroundVehicle.cxx +++ b/src/AIModel/AIGroundVehicle.cxx @@ -24,9 +24,8 @@ #include -#include
+#include #include -#include #include #include "AIGroundVehicle.hxx" @@ -37,20 +36,19 @@ FGAIShip(otGroundVehicle), _pitch(0), _pitch_deg(0), _speed_kt(0), -_selected_ac(0), _range_ft(0), _relbrg (0), _parent_speed(0), -_dt_count(0), -_next_run(0), _parent_x_offset(0), _parent_y_offset(0), _parent_z_offset(0), -_parent(""), +_dt_count(0), +_next_run(0), _break_count(0) { invisible = false; + _parent = ""; } FGAIGroundVehicle::~FGAIGroundVehicle() {} @@ -86,77 +84,72 @@ void FGAIGroundVehicle::readFromScenario(SGPropertyNode* scFileNode) { void FGAIGroundVehicle::bind() { FGAIShip::bind(); - props->tie("controls/constants/elevation-coeff", + tie("controls/constants/elevation-coeff", SGRawValuePointer(&_elevation_coeff)); - props->tie("controls/constants/pitch-coeff", + tie("controls/constants/pitch-coeff", SGRawValuePointer(&_pitch_coeff)); - props->tie("position/ht-AGL-ft", + tie("position/ht-AGL-ft", SGRawValuePointer(&_ht_agl_ft)); - props->tie("hitch/rel-bearing-deg", + tie("hitch/rel-bearing-deg", SGRawValuePointer(&_relbrg)); - props->tie("hitch/tow-angle-deg", + tie("hitch/tow-angle-deg", SGRawValuePointer(&_tow_angle)); - props->tie("hitch/range-ft", + tie("hitch/range-ft", SGRawValuePointer(&_range_ft)); - props->tie("hitch/x-offset-ft", + tie("hitch/x-offset-ft", SGRawValuePointer(&_x_offset)); - props->tie("hitch/y-offset-ft", + tie("hitch/y-offset-ft", SGRawValuePointer(&_y_offset)); - props->tie("hitch/z-offset-ft", + tie("hitch/z-offset-ft", SGRawValuePointer(&_z_offset)); - props->tie("hitch/parent-x-offset-ft", + tie("hitch/parent-x-offset-ft", SGRawValuePointer(&_parent_x_offset)); - props->tie("hitch/parent-y-offset-ft", + tie("hitch/parent-y-offset-ft", SGRawValuePointer(&_parent_y_offset)); - props->tie("hitch/parent-z-offset-ft", + tie("hitch/parent-z-offset-ft", SGRawValuePointer(&_parent_z_offset)); - props->tie("controls/constants/tow-angle/gain", + tie("controls/constants/tow-angle/gain", SGRawValuePointer(&_tow_angle_gain)); - props->tie("controls/constants/tow-angle/limit-deg", + tie("controls/constants/tow-angle/limit-deg", SGRawValuePointer(&_tow_angle_limit)); - props->tie("controls/contact-x1-offset-ft", + tie("controls/contact-x1-offset-ft", SGRawValuePointer(&_contact_x1_offset)); - props->tie("controls/contact-x2-offset-ft", + tie("controls/contact-x2-offset-ft", SGRawValuePointer(&_contact_x2_offset)); } -void FGAIGroundVehicle::unbind() { - FGAIShip::unbind(); - - props->untie("controls/constants/elevation-coeff"); - props->untie("controls/constants/pitch-coeff"); - props->untie("position/ht-AGL-ft"); - props->untie("hitch/rel-bearing-deg"); - props->untie("hitch/tow-angle-deg"); - props->untie("hitch/range-ft"); - props->untie("hitch/x-offset-ft"); - props->untie("hitch/y-offset-ft"); - props->untie("hitch/z-offset-ft"); - props->untie("hitch/parent-x-offset-ft"); - props->untie("hitch/parent-y-offset-ft"); - props->untie("hitch/parent-y-offset-ft"); - props->untie("controls/constants/tow-angle/gain"); - props->untie("controls/constants/tow-angle/limit-deg"); - props->untie("controls/contact-x1-offset-ft"); - props->untie("controls/contact-x2-offset-ft"); -} - bool FGAIGroundVehicle::init(bool search_in_AI_path) { if (!FGAIShip::init(search_in_AI_path)) return false; + reinit(); + return true; +} +void FGAIGroundVehicle::reinit() { invisible = false; _limit = 200; no_roll = true; - - props->setStringValue("controls/parent-name", _parent.c_str()); - setParentNode(); - return true; + props->setStringValue("controls/parent-name", _parent.c_str()); + + if (setParentNode()){ + _parent_x_offset = _selected_ac->getDoubleValue("hitch/x-offset-ft"); + _parent_y_offset = _selected_ac->getDoubleValue("hitch/y-offset-ft"); + _parent_z_offset = _selected_ac->getDoubleValue("hitch/z-offset-ft"); + _hitch_x_offset_m = _selected_ac->getDoubleValue("hitch/x-offset-ft") + * SG_FEET_TO_METER; + _hitch_y_offset_m = _selected_ac->getDoubleValue("hitch/y-offset-ft") + * SG_FEET_TO_METER; + _hitch_z_offset_m = _selected_ac->getDoubleValue("hitch/z-offset-ft") + * SG_FEET_TO_METER; + setParent(); + } + + FGAIShip::reinit(); } void FGAIGroundVehicle::update(double dt) { - // SG_LOG(SG_GENERAL, SG_ALERT, "updating GroundVehicle: " << _name ); + // SG_LOG(SG_AI, SG_ALERT, "updating GroundVehicle: " << _name ); FGAIShip::update(dt); RunGroundVehicle(dt); @@ -212,10 +205,6 @@ void FGAIGroundVehicle::setPitch(double p, double dt, double coeff){ _pitch_deg = (p * c) + (_pitch_deg * (1 - c)); } -void FGAIGroundVehicle::setParentName(const string& p) { - _parent = p; -} - void FGAIGroundVehicle::setTowAngle(double ta, double dt, double coeff){ ta *= _tow_angle_gain; double factor = -0.0045 * speed + 1; @@ -225,39 +214,6 @@ void FGAIGroundVehicle::setTowAngle(double ta, double dt, double coeff){ SG_CLAMP_RANGE(_tow_angle, -limit, limit); } -bool FGAIGroundVehicle::getGroundElev(SGGeod inpos) { - - double height_m ; - - if (globals->get_scenery()->get_elevation_m(SGGeod::fromGeodM(inpos, 3000), height_m, &_material,0)){ - _ht_agl_ft = inpos.getElevationFt() - height_m * SG_METER_TO_FEET; - - if (_material) { - const vector& names = _material->get_names(); - - _solid = _material->get_solid(); - - if (!names.empty()) - props->setStringValue("material/name", names[0].c_str()); - else - props->setStringValue("material/name", ""); - - //cout << "material " << names[0].c_str() - // << " _elevation_m " << _elevation_m - // << " solid " << _solid - // << " load " << _load_resistance - // << " frictionFactor " << _frictionFactor - // << endl; - - } - - return true; - } else { - return false; - } - -} - bool FGAIGroundVehicle::getPitch() { if (!_tunnel){ @@ -278,16 +234,16 @@ bool FGAIGroundVehicle::getPitch() { double rear_elev_m = 0; double elev_front = 0; double elev_rear = 0; - double max_alt = 10000; + //double max_alt = 10000; if (globals->get_scenery()->get_elevation_m(SGGeod::fromGeodM(geodFront, 3000), - elev_front, &_material, 0)){ + elev_front, NULL, 0)){ front_elev_m = elev_front + _z_offset_m; } else return false; if (globals->get_scenery()->get_elevation_m(SGGeod::fromGeodM(geodRear, 3000), - elev_rear, &_material, 0)){ + elev_rear, NULL, 0)){ rear_elev_m = elev_rear; } else return false; @@ -307,7 +263,7 @@ bool FGAIGroundVehicle::getPitch() { } else { - if (prev->altitude == 0 || curr->altitude == 0) return false; + if (prev->getAltitude() == 0 || curr->getAltitude() == 0) return false; static double distance; static double d_alt; @@ -316,20 +272,20 @@ bool FGAIGroundVehicle::getPitch() { if (_new_waypoint){ //cout << "new waypoint, calculating pitch " << endl; - curr_alt = curr->altitude; - prev_alt = prev->altitude; + curr_alt = curr->getAltitude(); + prev_alt = prev->getAltitude(); //cout << "prev_alt" <altitude; - distance = SGGeodesy::distanceM(SGGeod::fromDeg(prev->longitude, prev->latitude), - SGGeod::fromDeg(curr->longitude, curr->latitude)); + distance = SGGeodesy::distanceM(SGGeod::fromDeg(prev->getLongitude(), prev->getLatitude()), + SGGeod::fromDeg(curr->getLongitude(), curr->getLatitude())); _pitch = atan2(d_alt, distance * SG_METER_TO_FEET) * SG_RADIANS_TO_DEGREES; //cout << "new waypoint, calculating pitch " << _pitch << // " " << _pitch_offset << " " << _elevation <longitude, curr->latitude)); + SGGeod::fromDeg(curr->getLongitude(), curr->getLatitude())); /*cout << "tunnel " << _tunnel << " distance prev & curr " << prev->name << " " << curr->name << " " << distance * SG_METER_TO_FEET @@ -344,62 +300,9 @@ bool FGAIGroundVehicle::getPitch() { } - //getGroundElev(pos); - return true; } -void FGAIGroundVehicle::setParentNode() { - - if(_parent == "") - return; - - const SGPropertyNode_ptr ai = fgGetNode("/ai/models", true); - - for (int i = ai->nChildren() - 1; i >= -1; i--) { - SGPropertyNode_ptr model; - - if (i < 0) { // last iteration: selected model - model = _selected_ac; - } else { - model = ai->getChild(i); - string path = ai->getPath(); - const string name = model->getStringValue("name"); - - if (!model->nChildren()){ - continue; - } - if (name == _parent) { - _selected_ac = model; // save selected model for last iteration - break; - } - - } - if (!model) - continue; - - }// end for loop - - if (_selected_ac != 0){ - const string name = _selected_ac->getStringValue("name"); - _parent_x_offset = _selected_ac->getDoubleValue("hitch/x-offset-ft"); - _parent_y_offset = _selected_ac->getDoubleValue("hitch/y-offset-ft"); - _parent_z_offset = _selected_ac->getDoubleValue("hitch/z-offset-ft"); - _hitch_x_offset_m = _selected_ac->getDoubleValue("hitch/x-offset-ft") - * SG_FEET_TO_METER; - _hitch_y_offset_m = _selected_ac->getDoubleValue("hitch/y-offset-ft") - * SG_FEET_TO_METER; - _hitch_z_offset_m = _selected_ac->getDoubleValue("hitch/z-offset-ft") - * SG_FEET_TO_METER; - setParent(); - } else { - SG_LOG(SG_GENERAL, SG_ALERT, "AIGroundVeh1cle: " << _name - << " parent not found: dying "); - setDie(true); - } - -} - void FGAIGroundVehicle::setParent(){ double lat = _selected_ac->getDoubleValue("position/latitude-deg"); @@ -437,12 +340,6 @@ void FGAIGroundVehicle::calcRangeBearing(double lat, double lon, double lat2, do range = distance * SG_METER_TO_NM; } -double FGAIGroundVehicle::calcRelBearingDeg(double bearing, double heading) -{ - double angle = bearing - heading; - SG_NORMALIZE_RANGE(angle, -180.0, 180.0); - return angle; -} SGVec3d FGAIGroundVehicle::getCartHitchPosAt(const SGVec3d& _off) const { double hdg = _selected_ac->getDoubleValue("orientation/true-heading-deg"); @@ -471,13 +368,13 @@ void FGAIGroundVehicle::AdvanceFP(){ double count = 0; string parent_next_name =_selected_ac->getStringValue("waypoint/name-next"); - while(fp->getNextWaypoint() != 0 && fp->getNextWaypoint()->name != "END" && count < 5){ - SG_LOG(SG_GENERAL, SG_DEBUG, "AIGroundVeh1cle: " << _name + while(fp->getNextWaypoint() != 0 && fp->getNextWaypoint()->getName() != "END" && count < 5){ + SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name <<" advancing waypoint to: " << parent_next_name); - if (fp->getNextWaypoint()->name == parent_next_name){ - SG_LOG(SG_GENERAL, SG_DEBUG, "AIGroundVeh1cle: " << _name - << " not setting waypoint already at: " << fp->getNextWaypoint()->name); + if (fp->getNextWaypoint()->getName() == parent_next_name){ + SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name + << " not setting waypoint already at: " << fp->getNextWaypoint()->getName()); return; } @@ -486,9 +383,9 @@ void FGAIGroundVehicle::AdvanceFP(){ curr = fp->getCurrentWaypoint(); next = fp->getNextWaypoint(); - if (fp->getNextWaypoint()->name == parent_next_name){ - SG_LOG(SG_GENERAL, SG_DEBUG, "AIGroundVeh1cle: " << _name - << " waypoint set to: " << fp->getNextWaypoint()->name); + if (fp->getNextWaypoint()->getName() == parent_next_name){ + SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name + << " waypoint set to: " << fp->getNextWaypoint()->getName()); return; } @@ -496,15 +393,15 @@ void FGAIGroundVehicle::AdvanceFP(){ }// end while loop - while(fp->getPreviousWaypoint() != 0 && fp->getPreviousWaypoint()->name != "END" + while(fp->getPreviousWaypoint() != 0 && fp->getPreviousWaypoint()->getName() != "END" && count > -10){ - SG_LOG(SG_GENERAL, SG_DEBUG, "AIGroundVeh1cle: " << _name + SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name << " retreating waypoint to: " << parent_next_name - << " at: " << fp->getNextWaypoint()->name); + << " at: " << fp->getNextWaypoint()->getName()); - if (fp->getNextWaypoint()->name == parent_next_name){ - SG_LOG(SG_GENERAL, SG_DEBUG, "AIGroundVeh1cle: " << _name - << " not setting waypoint already at:" << fp->getNextWaypoint()->name ); + if (fp->getNextWaypoint()->getName() == parent_next_name){ + SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name + << " not setting waypoint already at:" << fp->getNextWaypoint()->getName() ); return; } @@ -513,9 +410,9 @@ void FGAIGroundVehicle::AdvanceFP(){ curr = fp->getCurrentWaypoint(); next = fp->getNextWaypoint(); - if (fp->getNextWaypoint()->name == parent_next_name){ - SG_LOG(SG_GENERAL, SG_DEBUG, "AIGroundVeh1cle: " << _name - << " waypoint set to: " << fp->getNextWaypoint()->name); + if (fp->getNextWaypoint()->getName() == parent_next_name){ + SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name + << " waypoint set to: " << fp->getNextWaypoint()->getName()); return; } @@ -526,7 +423,7 @@ void FGAIGroundVehicle::AdvanceFP(){ void FGAIGroundVehicle::setTowSpeed(){ - double diff = _range_ft - _x_offset; + //double diff = _range_ft - _x_offset; double x = 0; if (_range_ft > _x_offset * 3) x = 50; @@ -574,7 +471,7 @@ void FGAIGroundVehicle::RunGroundVehicle(double dt){ } if(_parent == ""){ - AccelTo(prev->speed); + AccelTo(prev->getSpeed()); _dt_count = 0; return; } @@ -583,10 +480,10 @@ void FGAIGroundVehicle::RunGroundVehicle(double dt){ string parent_next_name = _selected_ac->getStringValue("waypoint/name-next"); bool parent_waiting = _selected_ac->getBoolValue("waypoint/waiting"); - bool parent_restart = _selected_ac->getBoolValue("controls/restart"); + //bool parent_restart = _selected_ac->getBoolValue("controls/restart"); - if (parent_next_name == "END" && fp->getNextWaypoint()->name != "END" ){ - SG_LOG(SG_GENERAL, SG_DEBUG, "AIGroundVeh1cle: " << _name + if (parent_next_name == "END" && fp->getNextWaypoint()->getName() != "END" ){ + SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name << " setting END: getting new waypoints "); AdvanceFP(); setWPNames(); @@ -594,21 +491,21 @@ void FGAIGroundVehicle::RunGroundVehicle(double dt){ if(_restart) _missed_count = 200; /*} else if (parent_next_name == "WAIT" && fp->getNextWaypoint()->name != "WAIT" ){*/ } else if (parent_waiting && !_waiting){ - SG_LOG(SG_GENERAL, SG_DEBUG, "AIGroundVeh1cle: " << _name + SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name << " setting WAIT/WAITUNTIL: getting new waypoints "); AdvanceFP(); setWPNames(); _waiting = true; - } else if (parent_next_name != "WAIT" && fp->getNextWaypoint()->name == "WAIT"){ - SG_LOG(SG_GENERAL, SG_DEBUG, "AIGroundVeh1cle: " << _name + } else if (parent_next_name != "WAIT" && fp->getNextWaypoint()->getName() == "WAIT"){ + SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name << " wait done: getting new waypoints "); _waiting = false; _wait_count = 0; fp->IncrementWaypoint(false); next = fp->getNextWaypoint(); - if (next->name == "WAITUNTIL" || next->name == "WAIT" - || next->name == "END"){ + if (next->getName() == "WAITUNTIL" || next->getName() == "WAIT" + || next->getName() == "END"){ } else { prev = curr; fp->IncrementWaypoint(false); @@ -619,7 +516,7 @@ void FGAIGroundVehicle::RunGroundVehicle(double dt){ setWPNames(); } else if (_range_ft > (_x_offset +_parent_x_offset)* 4 ){ - SG_LOG(SG_GENERAL, SG_ALERT, "AIGroundVeh1cle: " << _name + SG_LOG(SG_AI, SG_ALERT, "AIGroundVeh1cle: " << _name << " rescue: reforming train " << _range_ft );