File: RoutePlan.cpp

package info (click to toggle)
endless-sky 0.10.16-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 414,608 kB
  • sloc: cpp: 73,435; python: 893; xml: 666; sh: 271; makefile: 28
file content (115 lines) | stat: -rw-r--r-- 2,491 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* RoutePlan.cpp
Copyright (c) 2022 by alextd

Endless Sky is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.

Endless Sky is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "RoutePlan.h"

#include "DistanceMap.h"

using namespace std;



// RoutePlan is a wrapper on DistanceMap that uses destination
RoutePlan::RoutePlan(const System &center, const System &destination, const PlayerInfo *player)
{
	Init(DistanceMap(center, destination, player));
}



RoutePlan::RoutePlan(const Ship &ship, const System &destination, const PlayerInfo *player)
{
	Init(DistanceMap(ship, destination, player));
}



void RoutePlan::Init(const DistanceMap &distance)
{
	auto it = distance.route.find(distance.destination);
	if(it == distance.route.end())
		return;

	hasRoute = true;

	while(it->first != distance.center)
	{
		plan.emplace_back(it->first, it->second);
		it = distance.route.find(it->second.prev);
	}
}



// Find out if the destination is reachable.
bool RoutePlan::HasRoute() const
{
	return hasRoute;
}



// Get the first step on the route from center to the destination.
const System *RoutePlan::FirstStep() const
{
	if(!hasRoute)
		return nullptr;

	return plan.back().first;
}



// Find out how many days away the destination is.
int RoutePlan::Days() const
{
	if(!hasRoute)
		return -1;

	return plan.front().second.days;
}



// How much fuel is needed to travel to this system along the route.
int RoutePlan::RequiredFuel() const
{
	if(!hasRoute)
		return -1;

	return plan.front().second.fuel;
}



// Get the list of jumps to take to get to the destination.
vector<const System *> RoutePlan::Plan() const
{
	vector<const System *> steps;
	for(const auto &it : plan)
		steps.push_back(it.first);
	return steps;
}



// How much fuel is needed to travel to this system along the route.
vector<pair<const System *, int>> RoutePlan::FuelCosts() const
{
	vector<pair<const System *, int>> steps;
	for(const auto &it : plan)
		steps.emplace_back(it.first, it.second.fuel);
	return steps;
}