*: use more constexpr
[xcsoar.git] / src / Waypoint / HomeGlue.cpp
blobd1e8da197d644f8fb89aedcfcd666d00791ba360
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "WaypointGlue.hpp"
25 #include "ComputerSettings.hpp"
26 #include "Profile/Profile.hpp"
27 #include "Blackboard/DeviceBlackboard.hpp"
28 #include "LogFile.hpp"
29 #include "Terrain/RasterTerrain.hpp"
30 #include "Waypoint/Waypoints.hpp"
31 #include "LastUsed.hpp"
33 const Waypoint *
34 WaypointGlue::FindHomeId(Waypoints &waypoints,
35 PlacesOfInterestSettings &settings)
37 if (settings.home_waypoint < 0)
38 return NULL;
40 const Waypoint *wp = waypoints.LookupId(settings.home_waypoint);
41 if (wp == NULL) {
42 settings.home_waypoint = -1;
43 return NULL;
46 settings.home_location = wp->location;
47 settings.home_location_available = true;
48 waypoints.SetHome(wp->id);
49 return wp;
52 const Waypoint *
53 WaypointGlue::FindHomeLocation(Waypoints &waypoints,
54 PlacesOfInterestSettings &settings)
56 if (!settings.home_location_available)
57 return NULL;
59 const Waypoint *wp = waypoints.LookupLocation(settings.home_location,
60 fixed(100));
61 if (wp == NULL || !wp->IsAirport()) {
62 settings.home_location_available = false;
63 return NULL;
66 settings.home_waypoint = wp->id;
67 waypoints.SetHome(wp->id);
68 return wp;
71 const Waypoint *
72 WaypointGlue::FindFlaggedHome(Waypoints &waypoints,
73 PlacesOfInterestSettings &settings)
75 const Waypoint *wp = waypoints.FindHome();
76 if (wp == NULL)
77 return NULL;
79 settings.SetHome(*wp);
80 return wp;
83 void
84 WaypointGlue::SetHome(Waypoints &way_points, const RasterTerrain *terrain,
85 PlacesOfInterestSettings &poi_settings,
86 TeamCodeSettings &team_code_settings,
87 DeviceBlackboard *device_blackboard,
88 const bool reset)
90 if (reset)
91 poi_settings.home_waypoint = -1;
93 // check invalid home waypoint or forced reset due to file change
94 const Waypoint *wp = FindHomeId(way_points, poi_settings);
95 if (wp == NULL) {
96 /* fall back to HomeLocation, try to find it in the waypoint
97 database */
98 wp = FindHomeLocation(way_points, poi_settings);
99 if (wp == NULL)
100 // search for home in waypoint list, if we don't have a home
101 wp = FindFlaggedHome(way_points, poi_settings);
104 if (wp != NULL)
105 LastUsedWaypoints::Add(*wp);
107 // check invalid task ref waypoint or forced reset due to file change
108 if (reset || way_points.IsEmpty() ||
109 !way_points.LookupId(team_code_settings.team_code_reference_waypoint))
110 // set team code reference waypoint if we don't have one
111 team_code_settings.team_code_reference_waypoint = poi_settings.home_waypoint;
113 if (device_blackboard != NULL) {
114 if (wp != NULL) {
115 // OK, passed all checks now
116 LogFormat("Start at home waypoint");
117 device_blackboard->SetStartupLocation(wp->location, wp->elevation);
118 } else if (terrain != NULL) {
119 // no home at all, so set it from center of terrain if available
120 GeoPoint loc = terrain->GetTerrainCenter();
121 LogFormat("Start at terrain center");
122 device_blackboard->SetStartupLocation(loc,
123 fixed(terrain->GetTerrainHeight(loc)));
128 void
129 WaypointGlue::SaveHome(const PlacesOfInterestSettings &poi_settings,
130 const TeamCodeSettings &team_code_settings)
132 Profile::Set(ProfileKeys::HomeWaypoint, poi_settings.home_waypoint);
133 if (poi_settings.home_location_available)
134 Profile::SetGeoPoint(ProfileKeys::HomeLocation, poi_settings.home_location);
136 Profile::Set(ProfileKeys::TeamcodeRefWaypoint,
137 team_code_settings.team_code_reference_waypoint);