X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FAIModel%2Fsubmodel.cxx;h=a25212198584427cbcb3fa74c4013d6cce694153;hb=38226af24ec01e8f0a20d7fd73ef838a69f6ef25;hp=733bff3cb3130c62cc8828be0cd3d1055f689843;hpb=72c5a314e6daa5052165310a600c4a9a4c20e43f;p=flightgear.git diff --git a/src/AIModel/submodel.cxx b/src/AIModel/submodel.cxx index 733bff3cb..a25212198 100644 --- a/src/AIModel/submodel.cxx +++ b/src/AIModel/submodel.cxx @@ -28,12 +28,11 @@ const double FGSubmodelMgr::lbs_to_slugs = 0.031080950172; FGSubmodelMgr::FGSubmodelMgr() { - x_offset = y_offset = 0.0; - z_offset = -4.0; - pitch_offset = 2.0; + x_offset = y_offset = z_offset = 0.0; + pitch_offset = 0.0; yaw_offset = 0.0; - out[0] = out[1] = out[2] = 0; + //out[0] = out[1] = out[2] = 0; string contents_node; contrail_altitude = 30000; _count = 0; @@ -44,6 +43,11 @@ FGSubmodelMgr::~FGSubmodelMgr() { } +FGAIManager* FGSubmodelMgr::aiManager() +{ + return (FGAIManager*)globals->get_subsystem("ai_model"); +} + void FGSubmodelMgr::init() { index = 0; @@ -75,26 +79,22 @@ void FGSubmodelMgr::init() _contrail_trigger = fgGetNode("ai/submodels/contrails", true); _contrail_trigger->setBoolValue(false); - ai = (FGAIManager*)globals->get_subsystem("ai_model"); - load(); -// _model_added_node = fgGetNode("ai/models/model-added", true); - //_model_added_node->addChangeListener(this, false); - } void FGSubmodelMgr::postinit() { // postinit, so that the AI list is populated - loadAI(); + + loadAI(); while (_found_sub) loadSubmodels(); //TODO reload submodels if an MP ac joins - _model_added_node = fgGetNode("ai/models/model-added", true); - _model_added_node->addChangeListener(this, false); + //_model_added_node = fgGetNode("ai/models/model-added", true); + //_model_added_node->addChangeListener(this, false); } void FGSubmodelMgr::bind() @@ -119,22 +119,33 @@ void FGSubmodelMgr::update(double dt) _expiry = false; // check if the submodel hit an object or terrain - sm_list = ai->get_ai_list(); - sm_list_iterator sm_list_itr = sm_list.begin(); - sm_list_iterator end = sm_list.end(); + FGAIManager::ai_list_type sm_list(aiManager()->get_ai_list()); + FGAIManager::ai_list_iterator sm_list_itr = sm_list.begin(), + end = sm_list.end(); for (; sm_list_itr != end; ++sm_list_itr) { - _impact = (*sm_list_itr)->_getImpactData(); - _hit = (*sm_list_itr)->_getCollisionData(); - _expiry = (*sm_list_itr)->_getExpiryData(); + FGAIBase::object_type object_type =(*sm_list_itr)->getType(); + + if (object_type != FGAIBase::otBallistic){// only work on ballistic objects + continue; // so continue + } int parent_subID = (*sm_list_itr)->_getSubID(); + int id = (*sm_list_itr)->getID(); + + if ( parent_subID == 0 || id == -1) // this entry in the list has no associated submodel + continue; // or is invalid so we can continue //SG_LOG(SG_GENERAL, SG_DEBUG, "Submodel: Impact " << _impact << " hit! " // << _hit <<" parent_subID " << parent_subID); - if ( parent_subID == 0) // this entry in the list has no associated submodel - continue; // so we can continue + _hit = (*sm_list_itr)->_getCollisionData(); + _impact = (*sm_list_itr)->_getImpactData(); + _expiry = (*sm_list_itr)->_getExpiryData(); + + //SG_LOG(SG_GENERAL, SG_ALERT, "Submodel: " << (*sm_list_itr)->_getName() + // << " Impact " << _impact << " hit! " << _hit + // << " exipiry :-( " << _expiry ); if (_impact || _hit || _expiry) { // SG_LOG(SG_GENERAL, SG_ALERT, "Submodel: Impact " << _impact << " hit! " << _hit @@ -157,8 +168,10 @@ void FGSubmodelMgr::update(double dt) (*submodel_iterator)->first_time = true; //cout << "Impact: parent SubID = child_ID elev " << _parent_elev << endl; - if (release(*submodel_iterator, dt)) + if (release(*submodel_iterator, dt)){ (*sm_list_itr)->setDie(true); + //cout << "Impact: set die" << (*sm_list_itr)->_getName() << endl; + } } @@ -188,19 +201,20 @@ void FGSubmodelMgr::update(double dt) trigger = _trigger_node->getBoolValue(); //cout << (*submodel_iterator)->name << "trigger node found " << trigger << endl; } else { - trigger = true; - //cout << (*submodel_iterator)->name << "trigger node not found " << trigger << endl; + trigger = false; + //cout << (*submodel_iterator)->name << " trigger node not found " << trigger << endl; } if (trigger && (*submodel_iterator)->count != 0) { - int id = (*submodel_iterator)->id; + //int id = (*submodel_iterator)->id; string name = (*submodel_iterator)->name; SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels release: " << (*submodel_iterator)->id << " name " << (*submodel_iterator)->name << " count " << (*submodel_iterator)->count + << " slaved " << (*submodel_iterator)->slaved ); release(*submodel_iterator, dt); @@ -213,13 +227,20 @@ void FGSubmodelMgr::update(double dt) bool FGSubmodelMgr::release(submodel *sm, double dt) { - //cout << "release id " << sm->id << " name " << sm->name - //<< " first time " << sm->first_time << " repeat " << sm->repeat << - // endl; + //cout << "release id " << sm->id + // << " name " << sm->name + // << " first time " << sm->first_time + // << " repeat " << sm->repeat + // << " slaved " << sm->slaved + // << endl; // only run if first time or repeat is set to true if (!sm->first_time && !sm->repeat) { - //cout<< "not first time " << sm->first_time<< " repeat " << sm->repeat <name + // << " not first time " << sm->first_time + // << " repeat " << sm->repeat + // << " slaved " << sm->slaved + // << endl; return false; } @@ -244,6 +265,7 @@ bool FGSubmodelMgr::release(submodel *sm, double dt) FGAIBallistic* ballist = new FGAIBallistic; ballist->setPath(sm->model.c_str()); ballist->setName(sm->name); + ballist->setSlaved(sm->slaved); ballist->setRandom(sm->random); ballist->setRandomness(sm->randomness); ballist->setLatitude(offsetpos.getLatitudeDeg()); @@ -273,7 +295,16 @@ bool FGSubmodelMgr::release(submodel *sm, double dt) ballist->setForceStabilisation(sm->force_stabilised); ballist->setExternalForce(sm->ext_force); ballist->setForcePath(sm->force_path.c_str()); - ai->attach(ballist); + ballist->setXoffset(sm->x_offset); + ballist->setYoffset(sm->y_offset); + ballist->setZoffset(sm->z_offset); + ballist->setPitchoffset(sm->pitch_offset); + ballist->setYawoffset(sm->yaw_offset); + ballist->setParentNodes(_selected_ac); + ballist->setContentsNode(sm->contents_node); + ballist->setWeight(sm->weight); + + aiManager()->attach(ballist); if (sm->count > 0) sm->count--; @@ -295,7 +326,7 @@ void FGSubmodelMgr::load() void FGSubmodelMgr::transform(submodel *sm) { // set initial conditions - if (sm->contents_node != 0) { + if (sm->contents_node != 0 && !sm->slaved) { // get the weight of the contents (lbs) and convert to mass (slugs) sm->contents = sm->contents_node->getChild("level-lbs",0,1)->getDoubleValue(); //cout << "transform: contents " << sm->contents << endl; @@ -310,14 +341,14 @@ void FGSubmodelMgr::transform(submodel *sm) } else IC.mass = sm->weight * lbs_to_slugs; - // cout << "mass " << IC.mass << endl; + int id = sm->id; + //int sub_id = sm->sub_id; + string name = sm->name; + if (sm->speed_node != 0) sm->speed = sm->speed_node->getDoubleValue(); - int id = sm->id; - int sub_id = sm->sub_id; - string name = sm->name; //cout << " name " << name << " id " << id << " sub id" << sub_id << endl; @@ -325,6 +356,7 @@ void FGSubmodelMgr::transform(submodel *sm) if (_impact || _hit || _expiry) { // set the data for a submodel tied to a submodel + _count++; IC.lat = _parent_lat; @@ -355,45 +387,25 @@ void FGSubmodelMgr::transform(submodel *sm) } else { // set the data for a submodel tied to an AI Object //cout << " set the data for a submodel tied to an AI Object " << id << endl; - sm_list_iterator sm_list_itr = sm_list.begin(); - sm_list_iterator end = sm_list.end(); - - while (sm_list_itr != end) { - int parent_id = (*sm_list_itr)->getID(); - - if (id != parent_id) { - ++sm_list_itr; - continue; - } - - //cout << " AI found id " << id << " alt " << (*sm_list_itr)->_getElevationFt()<< endl; - IC.lat = (*sm_list_itr)->_getLatitude(); - IC.lon = (*sm_list_itr)->_getLongitude(); - IC.alt = (*sm_list_itr)->_getElevationFt(); - IC.roll = (*sm_list_itr)->_getRoll(); - IC.elevation = (*sm_list_itr)->_getPitch(); - IC.azimuth = (*sm_list_itr)->_getHeading(); - IC.speed = (*sm_list_itr)->_getSpeed() * SG_KT_TO_FPS; - IC.speed_down_fps = -(*sm_list_itr)->_getVS_fps(); - IC.speed_east_fps = (*sm_list_itr)->_get_speed_east_fps(); - IC.speed_north_fps = (*sm_list_itr)->_get_speed_north_fps(); - - break; - - ++sm_list_itr; - } + setParentNode(id); } - /*cout << "heading " << IC.azimuth << endl ; - cout << "speed down " << IC.speed_down_fps << endl ; - cout << "speed east " << IC.speed_east_fps << endl ; - cout << "speed north " << IC.speed_north_fps << endl ; - cout << "parent speed fps in" << IC.speed << "sm speed in " << sm->speed << endl ;*/ + //cout << "Submodel: setting IC "<< name << endl; + //cout << "heading " << IC.azimuth << endl ; + //cout << "speed down " << IC.speed_down_fps << endl ; + //cout << "speed east " << IC.speed_east_fps << endl ; + //cout << "speed north " << IC.speed_north_fps << endl ; + //cout << "parent speed fps in " << IC.speed << "sm speed in " << sm->speed << endl ; + //cout << "lat " << IC.lat; + //cout << "alt " << IC.alt << endl ; + // Set the Initial Conditions that are common to all types of parent IC.wind_from_east = _user_wind_from_east_node->getDoubleValue(); IC.wind_from_north = _user_wind_from_north_node->getDoubleValue(); +//cout << "wind e " << IC.wind_from_east << " n " << IC.wind_from_north << endl; + userpos.setLatitudeDeg(IC.lat); userpos.setLongitudeDeg(IC.lon); userpos.setElevationFt(IC.alt); @@ -440,7 +452,7 @@ void FGSubmodelMgr::transform(submodel *sm) // if speeds are low this calculation can become unreliable if (IC.speed > 1) { - //IC.azimuth = atan2(IC.total_speed_east, IC.total_speed_north) * SG_RADIANS_TO_DEGREES; + IC.azimuth = atan2(IC.total_speed_east, IC.total_speed_north) * SG_RADIANS_TO_DEGREES; // cout << "azimuth1 " << IC.azimuth<get_ai_list(); + FGAIManager::ai_list_type sm_list(aiManager()->get_ai_list()); if (sm_list.empty()) { SG_LOG(SG_GENERAL, SG_ALERT, "Submodels: Unable to read AI submodel list"); return; } - sm_list_iterator sm_list_itr = sm_list.begin(); - sm_list_iterator end = sm_list.end(); + FGAIManager::ai_list_iterator sm_list_itr = sm_list.begin(), + end = sm_list.end(); while (sm_list_itr != end) { string path = (*sm_list_itr)->_getSMPath(); @@ -501,9 +514,7 @@ void FGSubmodelMgr::setData(int id, string& path, bool serviceable) { SGPropertyNode root; - SGPath config(globals->get_fg_root()); - config.append(path); - SG_LOG(SG_GENERAL, SG_DEBUG, "setData: path " << path); + SGPath config = globals->resolve_aircraft_path(path); try { SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Trying to read AI submodels file: " << config.str()); @@ -556,8 +567,6 @@ void FGSubmodelMgr::setData(int id, string& path, bool serviceable) sm->random = entry_node->getBoolValue("random", false); sm->randomness = entry_node->getDoubleValue("randomness", 0.5); - - //cout << "sm->contents_node " << sm->contents_node << endl; if (sm->contents_node != 0) sm->contents = sm->contents_node->getDoubleValue(); @@ -586,12 +595,12 @@ void FGSubmodelMgr::setData(int id, string& path, bool serviceable) sm->prop->tie("sub-id", SGRawValuePointer(&(sm->sub_id))); sm->prop->tie("serviceable", SGRawValuePointer(&(sm->serviceable))); sm->prop->tie("random", SGRawValuePointer(&(sm->random))); + sm->prop->tie("slaved", SGRawValuePointer(&(sm->slaved))); string name = sm->name; sm->prop->setStringValue("name", name.c_str()); string submodel = sm->submodel; sm->prop->setStringValue("submodel", submodel.c_str()); - //cout << " set submodel path " << submodel << endl; string force_path = sm->force_path; sm->prop->setStringValue("force_path", force_path.c_str()); @@ -608,18 +617,15 @@ void FGSubmodelMgr::setData(int id, string& path, bool serviceable) void FGSubmodelMgr::setSubData(int id, string& path, bool serviceable) { SGPropertyNode root; - - SGPath config(globals->get_fg_root()); - config.append(path); - SG_LOG(SG_GENERAL, SG_ALERT, "setSubData: path " << path); + SGPath config = globals->resolve_aircraft_path(path); try { - SG_LOG(SG_GENERAL, SG_ALERT, + SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Trying to read AI submodels file: " << config.str()); readProperties(config.str(), &root); } catch (const sg_exception &) { - SG_LOG(SG_GENERAL, SG_DEBUG, + SG_LOG(SG_GENERAL, SG_ALERT, "Submodels: Unable to read AI submodels file: " << config.str()); return; } @@ -666,7 +672,6 @@ void FGSubmodelMgr::setSubData(int id, string& path, bool serviceable) sm->random = entry_node->getBoolValue("random", false); sm->randomness = entry_node->getDoubleValue("randomness", 0.5); - //cout << "sm->contents_node " << sm->contents_node << endl; if (sm->contents_node != 0) sm->contents = sm->contents_node->getDoubleValue(); @@ -694,11 +699,13 @@ void FGSubmodelMgr::setSubData(int id, string& path, bool serviceable) sm->prop->tie("sub-id", SGRawValuePointer(&(sm->sub_id))); sm->prop->tie("serviceable", SGRawValuePointer(&(sm->serviceable))); sm->prop->tie("random", SGRawValuePointer(&(sm->random))); + sm->prop->tie("slaved", SGRawValuePointer(&(sm->slaved))); + string name = sm->name; sm->prop->setStringValue("name", name.c_str()); string submodel = sm->submodel; - sm->prop->setStringValue("submodel", submodel.c_str()); + sm->prop->setStringValue("submodel-path", submodel.c_str()); // cout << " set submodel path AI" << submodel<< endl; string force_path = sm->force_path; @@ -726,10 +733,10 @@ void FGSubmodelMgr::loadSubmodels() if (!submodel.empty()) { //int id = (*submodel_iterator)->id; bool serviceable = true; - //SG_LOG(SG_GENERAL, SG_DEBUG, "found path sub sub " - // << submodel - // << " index " << index - // << "name " << (*submodel_iterator)->name); + SG_LOG(SG_GENERAL, SG_DEBUG, "found path sub sub " + << submodel + << " index " << index + << " name " << (*submodel_iterator)->name); if ((*submodel_iterator)->sub_id == 0){ (*submodel_iterator)->sub_id = index; @@ -744,6 +751,7 @@ void FGSubmodelMgr::loadSubmodels() subsubmodel_iterator = subsubmodels.begin(); while (subsubmodel_iterator != subsubmodels.end()) { + submodels.push_back(*subsubmodel_iterator); ++subsubmodel_iterator; } // end while @@ -752,14 +760,19 @@ void FGSubmodelMgr::loadSubmodels() //submodel_iterator = submodels.begin(); + //int subcount = 0; + //while (submodel_iterator != submodels.end()) { - //int id = (*submodel_iterator)->id; - //SG_LOG(SG_GENERAL, SG_DEBUG,"after pushback " - // << " id " << id - // << " name " << (*submodel_iterator)->name - // << " sub id " << (*submodel_iterator)->sub_id); + // int id = (*submodel_iterator)->id; + // subcount++; - //++submodel_iterator; + // SG_LOG(SG_GENERAL, SG_ALERT,"after pushback " + // << " parent id " << id + // << " name " << (*submodel_iterator)->name + // << " sub id " << (*submodel_iterator)->sub_id + // << " subcount "<< subcount); + + // ++submodel_iterator; //} } @@ -767,7 +780,6 @@ SGVec3d FGSubmodelMgr::getCartOffsetPos() const{ // convert geodetic positions to geocentered SGVec3d cartuserPos = SGVec3d::fromGeod(userpos); - // Transform to the right coordinate frame, configuration is done in // the x-forward, y-right, z-up coordinates (feet), computation // in the simulation usual body x-forward, y-right, z-down coordinates @@ -793,7 +805,6 @@ SGVec3d FGSubmodelMgr::getCartOffsetPos() const{ // Add the position offset of the user model to get the geocentered position SGVec3d offsetPos = cartuserPos + off; - return offsetPos; } @@ -802,60 +813,93 @@ void FGSubmodelMgr::setOffsetPos(){ SGVec3d cartoffsetPos = getCartOffsetPos(); SGGeodesy::SGCartToGeod(cartoffsetPos, offsetpos); + + //cout << "OFFSET POS" << offsetpos.getElevationFt(); + } void FGSubmodelMgr::valueChanged(SGPropertyNode *prop) { + return; // this isn't working atm const char* _model_added = _model_added_node->getStringValue(); basic_string ::size_type indexCh2b; string str2 = _model_added; - const char *cstr2b = "ballistic"; + const char *cstr2b = "multiplayer"; indexCh2b = str2.find( cstr2b, 0 ); if (indexCh2b != string::npos ){ // we will ignore Ballistic Objects - there are potentially too many - return; - } else { - //cout << "model added - " << str2 <<" now do something "<< endl; - SGPropertyNode *a_node = fgGetNode(_model_added, true ); - SGPropertyNode *sub_node = a_node->getChild("submodels", 0, true); + //cout << "Submodels: model added - " << str2 <<" read path "<< endl; + //return; + SGPropertyNode *a_node = fgGetNode(_model_added, true); + SGPropertyNode *sub_node = a_node->getChild("sim", 0, true); SGPropertyNode_ptr path_node = sub_node->getChild("path", 0, true); + SGPropertyNode_ptr callsign_node = a_node->getChild("callsign", 0, true); - string path = path_node->getStringValue(); + string callsign = callsign_node->getStringValue(); - if (path.empty()){ - // nothing to do - return - //cout << "subpath empty - return" << endl << endl; + //cout << "Submodels: model added - " << callsign <<" read callsign "<< endl; return; - } else { - //cout << "subpath not empty: " << path << endl << endl; - SGPropertyNode_ptr ident_node = a_node->getChild("id", 0, true); - int id = ident_node->getIntValue(); - - setData(id, path, true); - - _found_sub = true; - - while (_found_sub) - loadSubmodels(); + } else { + cout << "model added - " << str2 <<" returning "<< endl; + return; } +} +void FGSubmodelMgr::setParentNode(int id) { + 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"); + int parent_id = model->getIntValue("id"); + if (!model->nChildren()){ + continue; + } + if (parent_id == id) { + _selected_ac = model; // save selected model for last iteration + break; + } + } + if (!model) + continue; + }// end for loop + if (_selected_ac != 0){ + //cout << " parent node found"<< endl; + const string name = _selected_ac->getStringValue("name"); + IC.lat = _selected_ac->getDoubleValue("position/latitude-deg"); + IC.lon = _selected_ac->getDoubleValue("position/longitude-deg"); + IC.alt = _selected_ac->getDoubleValue("position/altitude-ft"); + IC.roll = _selected_ac->getDoubleValue("orientation/roll-deg"); + IC.elevation = _selected_ac->getDoubleValue("orientation/pitch-deg"); + IC.azimuth = _selected_ac->getDoubleValue("orientation/true-heading-deg"); + IC.speed = _selected_ac->getDoubleValue("velocities/true-airspeed-kt") * SG_KT_TO_FPS; + IC.speed_down_fps = -_selected_ac->getDoubleValue("velocities/vertical-speed-fps"); + IC.speed_east_fps = _selected_ac->getDoubleValue("velocities/speed-east-fps"); + IC.speed_north_fps = _selected_ac->getDoubleValue("velocities/speed-north-fps"); + //cout << name << " IC.speed " << IC.speed << endl; + } else { + SG_LOG(SG_GENERAL, SG_ALERT, "AISubmodel: parent node not found "); + } } // end of submodel.cxx