-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.h
127 lines (102 loc) · 3.28 KB
/
train.h
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
116
117
118
119
120
121
122
123
124
125
126
127
/*
* train.h
*
* Created on: Dec 6, 2015
* Author: pavel
*/
#ifndef TRAIN_H_
#define TRAIN_H_
#include "train_generator.h"
#include "track.h"
#include <simlib.h>
#include <assert.h>
class CTrain : public simlib3::Process
{
public:
/**
* \brief Event that periodically activates the train, letting it update its progress.
*/
class CProgressUpdateEvent : public Event
{
public:
static const unsigned FREQUENCY;
CProgressUpdateEvent(CTrain& train);
void Behavior();
private:
CTrain& m_Train;
};
CTrain(CTrainGenerator& generator,
unsigned scheduledStartTime,
unsigned scheduledTargetStationArrival,
unsigned scheduledMainStationArrival = 0,
unsigned scheduledMainStationDeparture = 0);
virtual ~CTrain();
unsigned GetTrackDuration() const;
unsigned GetTraveledMinutes() const;
unsigned GetDistanceFromMainStation();
bool IsGoingFromMainStation() const;
CTrainGenerator& GetGenerator();
protected:
void Travel(unsigned duration);
CTrainGenerator& m_Generator;
// own schedule times (computed by generator)
// start time
unsigned m_ScheduledStartTime;
// stop at main station
unsigned m_ScheduledMainStationArrival;
unsigned m_ScheduledMainStationDeparture;
// arrival at the target station
unsigned m_ScheduledTargetStationArrival;
// actual track duration according to the schedule
unsigned m_TrackDuration;
// minutes already traveled at the actual track
unsigned m_TraveledMinutes;
CTrack* m_pTrack;
bool m_DirFromMainStation;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
inline unsigned CTrain::GetTrackDuration() const
{
return m_TrackDuration;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline unsigned CTrain::GetTraveledMinutes() const
{
return m_TraveledMinutes;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline unsigned CTrain::GetDistanceFromMainStation()
{
// only defined if the train is on a track
assert(m_pTrack);
unsigned distance = 0;
if(m_TrackDuration > 0)
{
double percent = m_TraveledMinutes / (double)m_TrackDuration;
distance = m_pTrack->GetLength() * percent;
}
// revert
if(!m_DirFromMainStation)
{
distance = m_pTrack->GetLength() - distance;
}
DBG_LOG_T(GetGenerator().GetTrainTitle()
<< ":\tTrain Location: "
<< CTimeInterval::MinutesToTime(m_ScheduledStartTime)
<< " - "
<< CTimeInterval::MinutesToTime(m_ScheduledTargetStationArrival)
<< '\t' << CTimeInterval::MinutesToTime(m_ScheduledTargetStationArrival - m_ScheduledStartTime)
<< '\t' << distance << " / " << m_pTrack->GetLength());
return distance;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline bool CTrain::IsGoingFromMainStation() const
{
return m_DirFromMainStation;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline CTrainGenerator& CTrain::GetGenerator()
{
return m_Generator;
}
#endif /* TRAIN_H_ */