~ubuntu-branches/ubuntu/oneiric/flightgear/oneiric

« back to all changes in this revision

Viewing changes to src/AIModel/AIManager.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2006-01-05 12:48:11 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20060105124811-xsr6rtcrb88w19c8
Tags: 0.9.9-1ubuntu1
Resynchronise with Debian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
#include "AIStorm.hxx"
37
37
#include "AIThermal.hxx"
38
38
#include "AICarrier.hxx"
 
39
#include "AIStatic.hxx"
39
40
 
40
41
SG_USING_STD(list);
41
42
 
51
52
}
52
53
 
53
54
FGAIManager::~FGAIManager() {
54
 
  ai_list_itr = ai_list.begin();
 
55
  ai_list_iterator ai_list_itr = ai_list.begin();
55
56
  while(ai_list_itr != ai_list.end()) {
56
57
      (*ai_list_itr)->unbind();
57
58
      delete (*ai_list_itr);
74
75
      return;
75
76
 
76
77
  wind_from_down_node = fgGetNode("/environment/wind-from-down-fps", true);
77
 
 
78
 
  scenario_filename = root->getNode("scenario", true)->getStringValue();
79
 
  if (scenario_filename != "") processScenario( scenario_filename );
80
 
 
 
78
  wind_from_east_node  = fgGetNode("/environment/wind-from-east-fps",true);
 
79
  wind_from_north_node = fgGetNode("/environment/wind-from-north-fps",true);
 
80
 
 
81
  user_latitude_node  = fgGetNode("/position/latitude-deg", true);
 
82
  user_longitude_node = fgGetNode("/position/longitude-deg", true);
 
83
  user_altitude_node  = fgGetNode("/position/altitude-ft", true);
 
84
  user_heading_node   = fgGetNode("/orientation/heading-deg", true);
 
85
  user_pitch_node     = fgGetNode("/orientation/pitch-deg", true);
 
86
  user_yaw_node       = fgGetNode("/orientation/side-slip-deg", true);
 
87
  user_speed_node     = fgGetNode("/velocities/uBody-fps", true);
 
88
 
 
89
  for(int i = 0 ; i < root->nChildren() ; i++) {
 
90
    SGPropertyNode *aiEntry = root->getChild( i );
 
91
        if( !strcmp( aiEntry->getName(), "scenario" ) ) {
 
92
      scenario_filename = aiEntry->getStringValue();
 
93
      if (scenario_filename != "") processScenario( scenario_filename );
 
94
        }
 
95
  }
81
96
  initDone = true;
82
97
}
83
98
 
84
99
 
 
100
void FGAIManager::reinit() {
 
101
   update(0.0);
 
102
}
 
103
 
 
104
 
85
105
void FGAIManager::bind() {
86
106
   root = globals->get_props()->getNode("ai/models", true);
87
107
   root->tie("count", SGRawValuePointer<int>(&numObjects[0]));
105
125
 
106
126
        _dt = dt;
107
127
 
108
 
        ai_list_itr = ai_list.begin();
 
128
        ai_list_iterator ai_list_itr = ai_list.begin();
109
129
        while(ai_list_itr != ai_list.end()) {
110
130
                if ((*ai_list_itr)->getDie()) {      
111
131
                  tmgr->release((*ai_list_itr)->getID());
130
150
                }
131
151
                ++ai_list_itr;
132
152
        }
133
 
        wind_from_down_node->setDoubleValue( strength );
 
153
        wind_from_down_node->setDoubleValue( strength ); // for thermals
134
154
 
135
155
}
136
156
 
156
176
        } else {
157
177
          ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
158
178
        }
 
179
        ai_plane->setAcType(entity->acType);
 
180
        ai_plane->setCompany(entity->company);
159
181
        ai_plane->setHeading(entity->heading);
160
182
        ai_plane->setSpeed(entity->speed);
161
183
        ai_plane->setPath(entity->path.c_str());
167
189
        if ( entity->fp ) {
168
190
          ai_plane->SetFlightPlan(entity->fp);
169
191
        }
170
 
 
 
192
        if (entity->repeat) {
 
193
          ai_plane->GetFlightPlan()->setRepeat(true);
 
194
        }
171
195
        ai_plane->init();
172
196
        ai_plane->bind();
173
197
        return ai_plane;
188
212
        ai_ship->setAltitude(entity->altitude);
189
213
        ai_ship->setLongitude(entity->longitude);
190
214
        ai_ship->setLatitude(entity->latitude);
191
 
        ai_ship->setBank(entity->rudder);
 
215
        ai_ship->setRudder(entity->rudder);
 
216
        ai_ship->setName(entity->name);
192
217
 
193
218
        if ( entity->fp ) {
194
219
           ai_ship->setFlightPlan(entity->fp);
207
232
        FGAICarrier* ai_carrier = new FGAICarrier(this);
208
233
        ai_list.push_back(ai_carrier);
209
234
        ++numObjects[0];
210
 
        ++numObjects[FGAIBase::otShip];
 
235
        ++numObjects[FGAIBase::otCarrier];
211
236
        ai_carrier->setHeading(entity->heading);
212
237
        ai_carrier->setSpeed(entity->speed);
213
238
        ai_carrier->setPath(entity->path.c_str());
214
239
        ai_carrier->setAltitude(entity->altitude);
215
240
        ai_carrier->setLongitude(entity->longitude);
216
241
        ai_carrier->setLatitude(entity->latitude);
217
 
        ai_carrier->setBank(entity->rudder);
 
242
        ai_carrier->setRudder(entity->rudder);
218
243
        ai_carrier->setSolidObjects(entity->solid_objects);
219
244
        ai_carrier->setWireObjects(entity->wire_objects);
220
245
        ai_carrier->setCatapultObjects(entity->catapult_objects);
 
246
        ai_carrier->setParkingPositions(entity->ppositions);
221
247
        ai_carrier->setRadius(entity->radius);
222
 
        ai_carrier->setXoffset(entity->x_offset);
223
 
        ai_carrier->setYoffset(entity->y_offset);
224
 
        ai_carrier->setZoffset(entity->z_offset);
 
248
        ai_carrier->setSign(entity->pennant_number);
 
249
        ai_carrier->setName(entity->name);
 
250
        ai_carrier->setFlolsOffset(entity->flols_offset);
 
251
        ai_carrier->setWind_from_east(entity->wind_from_east);
 
252
        ai_carrier->setWind_from_north(entity->wind_from_north);
 
253
        ai_carrier->setTACANChannelID(entity->TACAN_channel_ID);
 
254
        ai_carrier->setMaxLat(entity->max_lat);
 
255
        ai_carrier->setMinLat(entity->min_lat);
 
256
        ai_carrier->setMaxLong(entity->max_long);
 
257
        ai_carrier->setMinLong(entity->min_long);
 
258
        
225
259
 
226
260
        if ( entity->fp ) {
227
261
           ai_carrier->setFlightPlan(entity->fp);
270
304
        ai_storm->setHeading(entity->heading);
271
305
        ai_storm->setSpeed(entity->speed);
272
306
        ai_storm->setPath(entity->path.c_str());
273
 
        ai_storm->setAltitude(entity->altitude);
 
307
        ai_storm->setAltitude(entity->altitude); 
 
308
        ai_storm->setDiameter(entity->diameter / 6076.11549); 
 
309
        ai_storm->setHeight(entity->height_msl); 
 
310
        ai_storm->setStrengthNorm(entity->turb_strength); 
274
311
        ai_storm->setLongitude(entity->longitude);
275
312
        ai_storm->setLatitude(entity->latitude);
276
313
        ai_storm->init();
289
326
        ai_thermal->setLatitude(entity->latitude);
290
327
        ai_thermal->setMaxStrength(entity->strength);
291
328
        ai_thermal->setDiameter(entity->diameter / 6076.11549);
 
329
        ai_thermal->setHeight(entity->height_msl);
292
330
        ai_thermal->init();
293
331
        ai_thermal->bind();
294
332
        ai_list.push_back(ai_thermal);
295
333
        return ai_thermal;
296
334
}
297
335
 
298
 
void FGAIManager::destroyObject( void* ID ) {
299
 
        ai_list_itr = ai_list.begin();
 
336
void*
 
337
FGAIManager::createStatic( FGAIModelEntity *entity ) {
 
338
     
 
339
     // cout << "creating static object" << endl;    
 
340
 
 
341
        FGAIStatic* ai_static = new FGAIStatic(this);
 
342
        ai_list.push_back(ai_static);
 
343
        ++numObjects[0];
 
344
        ++numObjects[FGAIBase::otStatic];
 
345
        ai_static->setHeading(entity->heading);
 
346
        ai_static->setPath(entity->path.c_str());
 
347
        ai_static->setAltitude(entity->altitude);
 
348
        ai_static->setLongitude(entity->longitude);
 
349
        ai_static->setLatitude(entity->latitude);
 
350
        ai_static->init();
 
351
        ai_static->bind();
 
352
        return ai_static;
 
353
}
 
354
 
 
355
void FGAIManager::destroyObject( int ID ) {
 
356
        ai_list_iterator ai_list_itr = ai_list.begin();
300
357
        while(ai_list_itr != ai_list.end()) {
301
358
            if ((*ai_list_itr)->getID() == ID) {
302
359
              --numObjects[0];
311
368
        }
312
369
}
313
370
 
314
 
// fetch the user's state every 10 sim cycles
 
371
 
315
372
void FGAIManager::fetchUserState( void ) {
316
 
   ++dt_count;
317
 
   if (dt_count == 10) {
318
 
     user_latitude  = fgGetDouble("/position/latitude-deg");
319
 
     user_longitude = fgGetDouble("/position/longitude-deg");
320
 
     user_altitude  = fgGetDouble("/position/altitude-ft");
321
 
     user_heading   = fgGetDouble("/orientation/heading-deg");
322
 
     user_pitch     = fgGetDouble("/orientation/pitch-deg");
323
 
     user_yaw       = fgGetDouble("/orientation/side-slip-deg");
324
 
     user_speed     = fgGetDouble("/velocities/uBody-fps") * 0.592484;
325
 
     dt_count = 0;
326
 
   }
 
373
     user_latitude  = user_latitude_node->getDoubleValue();
 
374
     user_longitude = user_longitude_node->getDoubleValue();
 
375
     user_altitude  = user_altitude_node->getDoubleValue();
 
376
     user_heading   = user_heading_node->getDoubleValue();
 
377
     user_pitch     = user_pitch_node->getDoubleValue();
 
378
     user_yaw       = user_yaw_node->getDoubleValue();
 
379
     user_speed     = user_speed_node->getDoubleValue() * 0.592484;
 
380
     wind_from_east = wind_from_east_node->getDoubleValue();
 
381
     wind_from_north = wind_from_north_node->getDoubleValue();
 
382
     
 
383
     
327
384
}
328
385
 
329
386
 
337
394
}
338
395
 
339
396
 
340
 
void FGAIManager::processScenario( string &filename ) {
 
397
void FGAIManager::processScenario( const string &filename ) {
341
398
  FGAIScenario* s = new FGAIScenario( filename );
342
399
  for (int i=0;i<s->nEntries();i++) {
343
 
    FGAIModelEntity* en = s->getNextEntry();
 
400
   FGAIModelEntity* const en = s->getNextEntry();
344
401
 
345
402
    if (en) {
346
403
      if ( en->m_type == "aircraft") {
360
417
 
361
418
      } else if ( en->m_type == "ballistic") {
362
419
        createBallistic( en );
363
 
      }      
 
420
 
 
421
      } else if ( en->m_type == "static") {
 
422
        createStatic( en );
 
423
      }            
364
424
    }
365
425
  }
366
426
 
370
430
// This code keeps track of models that have already been loaded
371
431
// Eventually we'd prbably need to find a way to keep track of models
372
432
// that are unloaded again
373
 
ssgBranch * FGAIManager::getModel(const string& path)
 
433
ssgBranch * FGAIManager::getModel(const string& path) const
374
434
{
375
435
  ModelVecIterator i = loadedModels.begin();
376
436
  while (i != loadedModels.end())
384
444
 
385
445
void FGAIManager::setModel(const string& path, ssgBranch *model)
386
446
{
 
447
  if (model)
 
448
    model->ref();
387
449
  loadedModels.push_back(FGModelID(path,model));
388
450
}
389
451
 
 
452
bool FGAIManager::getStartPosition(const string& id, const string& pid,
 
453
                                   Point3D& geodPos, double& heading,
 
454
                                   sgdVec3 uvw)
 
455
{
 
456
  SGPropertyNode* root = fgGetNode("sim/ai", true);
 
457
  if (!root->getNode("enabled", true)->getBoolValue())
 
458
      return 0;
 
459
 
 
460
  bool found = false;
 
461
  for(int i = 0 ; (!found) && i < root->nChildren() ; i++) {
 
462
    SGPropertyNode *aiEntry = root->getChild( i );
 
463
        if( !strcmp( aiEntry->getName(), "scenario" ) ) {
 
464
        string filename = aiEntry->getStringValue();
 
465
        FGAIScenario* s = new FGAIScenario( filename );
 
466
        for (int i=0; i<s->nEntries(); i++) {
 
467
            FGAIModelEntity* en = s->getNextEntry();
 
468
            if (en && en->m_type == "carrier" &&
 
469
                (en->pennant_number == id || en->name == id)) {
 
470
            FGAICarrier* ai_carrier = new FGAICarrier(0);
 
471
            ai_carrier->setHeading(en->heading);
 
472
            ai_carrier->setSpeed(en->speed);
 
473
            ai_carrier->setAltitude(en->altitude);
 
474
            ai_carrier->setLongitude(en->longitude);
 
475
            ai_carrier->setLatitude(en->latitude);
 
476
            ai_carrier->setBank(en->rudder);
 
477
            ai_carrier->setParkingPositions(en->ppositions);
 
478
      ai_carrier->setWind_from_east(en->wind_from_east);
 
479
      ai_carrier->setWind_from_north(en->wind_from_north);
 
480
      //ai_carrier->setTACANFreq(en->TACAN_freq);
 
481
 
 
482
            if (ai_carrier->getParkPosition(pid, geodPos, heading, uvw)) {
 
483
                delete ai_carrier;
 
484
                found = true;
 
485
                break;
 
486
            }
 
487
 
 
488
            delete ai_carrier;
 
489
        }
 
490
      }
 
491
      delete s;
 
492
    }
 
493
  }
 
494
  return found;
 
495
}
390
496
 
391
497
//end AIManager.cxx