Debian Codegraph @

src/game/race.hpp

raw symbols SCIP
1 // This file is part of Dust Racing 2D.
2 // Copyright (C) 2015 Jussi Lind <[email protected]>
3 //
4 // Dust Racing 2D is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 // Dust Racing 2D is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>.
15
16 #ifndef RACE_HPP
17 #define RACE_HPP
18
19 #include <MCObject>
20 #include <QObject>
21 #include <QString>
22 #include <QTimer>
23
24 #include <chrono>
25 #include <memory>
26 #include <optional>
27 #include <unordered_map>
28 #include <vector>
29
30 #include "audiosource.hpp"
31 #include "timing.hpp"
32 #include "tracktile.hpp"
33
34 class Car;
35 class Game;
36 class OffTrackDetector;
37 class Route;
38 class Track;
39
40 //! Class that controls the race event, checkpoints and timing.
41 class Race : public AudioSource
42 {
43 Q_OBJECT
44
45 public:
46 Race(Game & game, size_t numCars);
47 virtual ~Race() override;
48
49 using TrackS = std::shared_ptr<Track>;
50 void initialize(TrackS track, size_t lapCount);
51
52 bool started() const;
53
54 size_t lapCount() const;
55
56 void addCar(Car & car);
57 void removeCars();
58
59 using TimingW = std::weak_ptr<Timing>;
60 TimingW timing() const;
61
62 bool checkeredFlagEnabled() const;
63
64 size_t getCurrentTargetNodeIndex(size_t carIndex) const;
65
66 Car & getLoser() const;
67 Car & getLeader() const;
68
69 size_t position(size_t carIndex) const;
70
71 signals:
72
73 void finished();
74 void messageRequested(QString message);
75 void tiresChanged(const Car & car);
76
77 void lapRecordAchieved(int msec);
78 void raceRecordAchieved(int msec);
79
80 public slots:
81
82 void start();
83
84 void stopEngineSounds();
85
86 void update(std::chrono::milliseconds timeStep);
87
88 void pitStop(Car & car);
89
90 private:
91 void checkForNewBestPosition(const Car & car);
92 void checkIfCarIsStuck(Car & car);
93 void checkIfCarIsOffTrack(Car & car);
94 void checkIfLapIsCompleted(Car & car, const Route & route, size_t currentTargetNodeIndex);
95
96 void clearPositions();
97 void clearRaceFlags();
98
99 void createStartGridObjects();
100
101 void initCars();
102 void initTiming();
103
104 bool isRaceFinished() const;
105
106 void moveCarOntoPreviousCheckPoint(Car & car);
107
108 void setTrack(TrackS track, size_t lapCount);
109
110 void translateCarsToStartPositions();
111
112 void updateRouteProgress(Car & car);
113 void updatePositions();
114
115 const size_t m_humanPlayerIndex1;
116 const size_t m_humanPlayerIndex2;
117
118 // Position required to unlock a new track
119 const size_t m_unlockLimit = 6;
120
121 using CarVector = std::vector<Car *>;
122 CarVector m_cars;
123
124 // Used only to keep references to the start grid objects.
125 // They are common for all races and created only once.
126 using StartGridObjectVector = std::vector<MCObjectPtr>;
127 StartGridObjectVector m_startGridObjects;
128
129 // Map from car route progression to car order vector.
130 // Tracks the order of the cars in the route.
131 using ProgressionHash = std::unordered_map<size_t, std::vector<size_t>>;
132 mutable ProgressionHash m_progression;
133
134 using OffTrackDetectorS = std::shared_ptr<OffTrackDetector>;
135 using OTDVector = std::vector<OffTrackDetectorS>;
136 OTDVector m_offTrackDetectors;
137
138 // Data structure to determine if a car is stuck.
139 // In that case we move the car onto the previous check point.
140 using StuckTileCounter = std::pair<TrackTileS, int>; // Tile pointer and counter.
141 using StuckHash = std::unordered_map<size_t, StuckTileCounter>; // Car index to StuckTileCounter.
142 StuckHash m_stuckHash;
143
144 struct CarStatus
145 {
146 size_t currentTargetNodeIndex = 0;
147 size_t nextTargetNodeIndex = 0;
148 size_t prevTargetNodeIndex = 0;
149
150 size_t routeProgression = 0;
151
152 size_t position = 0;
153 };
154 using StatusHash = std::unordered_map<size_t, CarStatus>; // Car index to CarStatus.
155 StatusHash m_statusHash;
156
157 const size_t m_numCars;
158
159 size_t m_lapCount;
160
161 using TimingS = std::shared_ptr<Timing>;
162 TimingS m_timing;
163 TrackS m_track;
164
165 bool m_started = false;
166 bool m_checkeredFlagEnabled = false;
167 bool m_winnerFinished = false;
168 bool m_isfinishedSignalSent = false;
169
170 std::optional<size_t> m_bestPos;
171
172 QTimer m_offTrackMessageTimer;
173
174 int m_offTrackCounter = 0;
175
176 Game & m_game;
177 };
178
179 #endif // RACE_HPP