| 1 | /* |
| 2 | * Copyright (C) 2020 James Turner |
| 3 | * |
| 4 | * This file is part of the program FlightGear. |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "test_AIManager.hxx" |
| 21 | |
| 22 | #include <cstring> |
| 23 | #include <memory> |
| 24 | |
| 25 | #include "test_suite/FGTestApi/NavDataCache.hxx" |
| 26 | #include "test_suite/FGTestApi/TestDataLogger.hxx" |
| 27 | #include "test_suite/FGTestApi/TestPilot.hxx" |
| 28 | #include "test_suite/FGTestApi/testGlobals.hxx" |
| 29 | |
| 30 | #include <AIModel/AIAircraft.hxx> |
| 31 | #include <AIModel/AIFlightPlan.hxx> |
| 32 | #include <AIModel/AIManager.hxx> |
| 33 | |
| 34 | #include <Airports/airport.hxx> |
| 35 | #include <Main/fg_props.hxx> |
| 36 | #include <Main/globals.hxx> |
| 37 | #include <Navaids/NavDataCache.hxx> |
| 38 | #include <Navaids/navrecord.hxx> |
| 39 | |
| 40 | ///////////////////////////////////////////////////////////////////////////// |
| 41 | |
| 42 | // Set up function for each test. |
| 43 | void AIManagerTests::setUp() |
| 44 | { |
| 45 | FGTestApi::setUp::initTestGlobals("AI"); |
| 46 | FGTestApi::setUp::initNavDataCache(); |
| 47 | |
| 48 | globals->get_subsystem_mgr()->add<FGAIManager>(); |
| 49 | |
| 50 | auto props = globals->get_props(); |
| 51 | props->setBoolValue("sim/ai/enabled", true); |
| 52 | |
| 53 | globals->get_subsystem_mgr()->bind(); |
| 54 | globals->get_subsystem_mgr()->init(); |
| 55 | globals->get_subsystem_mgr()->postinit(); |
| 56 | } |
| 57 | |
| 58 | // Clean up after each test. |
| 59 | void AIManagerTests::tearDown() |
| 60 | { |
| 61 | FGTestApi::tearDown::shutdownTestGlobals(); |
| 62 | } |
| 63 | |
| 64 | void AIManagerTests::testBasic() |
| 65 | { |
| 66 | auto aim = globals->get_subsystem<FGAIManager>(); |
| 67 | |
| 68 | auto bikf = FGAirport::findByIdent("BIKF"); |
| 69 | |
| 70 | auto pilot = SGSharedPtr<FGTestApi::TestPilot>(new FGTestApi::TestPilot); |
| 71 | FGTestApi::setPosition(bikf->geod()); |
| 72 | pilot->resetAtPosition(bikf->geod()); |
| 73 | |
| 74 | |
| 75 | pilot->setSpeedKts(220); |
| 76 | pilot->setCourseTrue(0.0); |
| 77 | pilot->setTargetAltitudeFtMSL(10000); |
| 78 | |
| 79 | FGTestApi::runForTime(10.0); |
| 80 | |
| 81 | auto aiUserAircraft = aim->getUserAircraft(); |
| 82 | CPPUNIT_ASSERT(aiUserAircraft->isValid()); |
| 83 | CPPUNIT_ASSERT(!aiUserAircraft->getDie()); |
| 84 | |
| 85 | const SGGeod g = globals->get_aircraft_position(); |
| 86 | CPPUNIT_ASSERT_DOUBLES_EQUAL(g.getLongitudeDeg(), aiUserAircraft->getGeodPos().getLongitudeDeg(), 0.01); |
| 87 | CPPUNIT_ASSERT_DOUBLES_EQUAL(g.getLatitudeDeg(), aiUserAircraft->getGeodPos().getLatitudeDeg(), 0.01); |
| 88 | |
| 89 | // disable, the AI user aircraft doesn't track altitude?! |
| 90 | // CPPUNIT_ASSERT_DOUBLES_EQUAL(g.getElevationFt(), aiUserAircraft->getGeodPos().getElevationFt(), 1); |
| 91 | CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("orientation/heading-deg"), aiUserAircraft->getTrueHeadingDeg(), 1); |
| 92 | CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("velocities/groundspeed-kt"), aiUserAircraft->getSpeed(), 1); |
| 93 | } |
| 94 | |
| 95 | // test for AIFLightPlan leg / legEnd pieces. |
| 96 | |
| 97 | void AIManagerTests::testAircraftWaypoints() |
| 98 | { |
| 99 | auto aim = globals->get_subsystem<FGAIManager>(); |
| 100 | |
| 101 | SGPropertyNode_ptr aircraftDefinition(new SGPropertyNode); |
| 102 | aircraftDefinition->setStringValue("type", "aircraft"); |
| 103 | aircraftDefinition->setStringValue("callsign", "G-ARTA"); |
| 104 | // set class for performance data |
| 105 | |
| 106 | |
| 107 | auto eggd = FGAirport::findByIdent("EGGD"); |
| 108 | aircraftDefinition->setDoubleValue("heading", 90.0); |
| 109 | aircraftDefinition->setDoubleValue("latitude", eggd->geod().getLatitudeDeg()); |
| 110 | aircraftDefinition->setDoubleValue("longitude", eggd->geod().getLongitudeDeg()); |
| 111 | aircraftDefinition->setDoubleValue("altitude", 6000.0); |
| 112 | aircraftDefinition->setDoubleValue("speed", 250.0); // IAS or TAS? |
| 113 | |
| 114 | FGTestApi::setPositionAndStabilise(eggd->geod()); |
| 115 | |
| 116 | auto ai = aim->addObject(aircraftDefinition); |
| 117 | CPPUNIT_ASSERT(ai); |
| 118 | CPPUNIT_ASSERT_EQUAL(FGAIBase::object_type::otAircraft, ai->getType()); |
| 119 | CPPUNIT_ASSERT_EQUAL(std::string{"aircraft"}, std::string{ai->getTypeString()}); |
| 120 | |
| 121 | auto aiAircraft = static_cast<FGAIAircraft*>(ai.get()); |
| 122 | |
| 123 | const auto aiPos = aiAircraft->getGeodPos(); |
| 124 | CPPUNIT_ASSERT_DOUBLES_EQUAL(eggd->geod().getLatitudeDeg(), aiPos.getLatitudeDeg(), 0.01); |
| 125 | CPPUNIT_ASSERT_DOUBLES_EQUAL(eggd->geod().getLongitudeDeg(), aiPos.getLongitudeDeg(), 0.01); |
| 126 | CPPUNIT_ASSERT_DOUBLES_EQUAL(90.0, aiAircraft->getTrueHeadingDeg(), 1); |
| 127 | CPPUNIT_ASSERT_DOUBLES_EQUAL(250.0, aiAircraft->getSpeed(), 1); |
| 128 | |
| 129 | std::unique_ptr<FGAIFlightPlan> aiFP(new FGAIFlightPlan); |
| 130 | ai->setFlightPlan(std::move(aiFP)); |
| 131 | } |