-
Notifications
You must be signed in to change notification settings - Fork 0
/
Freighter.cpp
102 lines (89 loc) · 2.43 KB
/
Freighter.cpp
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
//
// Created by Admin on 6/18/2021.
//
#include <cmath>
#include "Freighter.h"
// Stopped, Docked, Dead, MovingTo, MovingOnCourse
void Freighter::update() {
/* check if the fuel is equal or less than 0
* if so -> don't do move the ship and update the state of the ship to "stopped" */
if (_fuel <= 0 && _currentState != Dead) {
setCurrentState(Stopped);
return;
}
switch (_currentState) {
/* if the ship is "stopped" , "docked" or "dead" then don't update the ship location */
case Stopped:
return;
break;
case Docked:
return;
break;
case Dead:
return;
break;
case MovingTo:
_fuel -= (int) getSpeed() * FREIGHTER_CONSUMPTION;
MoveAgent(_position, _currentDestination, getSpeed());
break;
case MovingOnCourse:
_fuel -= (int) getSpeed() * FREIGHTER_CONSUMPTION;
MoveAgent(_position, _currentDestination, getSpeed());
break;
default:
break;
}
}
void Freighter::concreteStatus() {
std::cout << "Freighter " << getName() << " at ";
getPosition().print();
cout << ", fuel: " << ((_fuel > 0) ? _fuel : 0);
cout << ", resistance: " << _resistance;
std::cout << ", ";
this->printStatus("");
std::cout << " ,speed " << std::setprecision(4) << std::round(getSpeed())
<< " nm/hr";
cout << ", Containers: " << _containers << endl;
}
void Freighter::refuel(const double &fuel) {
if (_currentState != Docked)
throw "The ship isn't at a port at the moment";
if (fuel < 0)
throw "Not a valid amount of fuel at the port";
int needed_fuel = FREIGHTER_FUEL_TANK_CAPACITY - fuel;
if (needed_fuel <= fuel) {
_fuel += needed_fuel;
} else if (needed_fuel > fuel) {
_fuel += fuel;
}
}
void Freighter::dock() {
if (_currentState == Dead || _currentState == Stopped)
throw "Can't dock at the moment";
/*
//checks if ship is near any port
for (auto & i : Model::get_Instance().getPortVector()){
if (inProximity(this->_position,i->getPosition())){
_currentState = Docked;
_currentPort=i->getName();
}
}
*/
return;
}
void Freighter::load(const int &containers) {
if (_currentState != Docked)
throw "The ship isn't at a port at the moment";
_containers += containers;
return;
}
void Freighter::unload(const int &containers) {
if (_currentState != Docked)
throw "The ship isn't at a port at the moment";
if (containers > _containers)
throw "The number of requested _containers is more than what the ship has at the moment";
_containers -= containers;
}
void Freighter::status() {
this->concreteStatus();
}