Debian Codegraph @

src/game/ai.cpp

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 #include "ai.hpp"
17 #include "../common/route.hpp"
18 #include "../common/tracktilebase.hpp"
19 #include "car.hpp"
20 #include "race.hpp"
21 #include "track.hpp"
22 #include "trackdata.hpp"
23 #include "tracktile.hpp"
24
25 #include <MCRandom>
26 #include <MCTrigonom>
27
28 AI::AI(Car & car, std::shared_ptr<Race> race)
29 : m_car(car)
30 , m_race(race)
31 , m_lastDiff(0)
32 , m_lastTargetNodeIndex(0)
33 {
34 }
35
36 Car & AI::car() const
37 {
38 return m_car;
39 }
40
41 void AI::update(bool isRaceCompleted)
42 {
43 if (m_track)
44 {
45 if (m_lastTargetNodeIndex != m_race->getCurrentTargetNodeIndex(m_car.index()))
46 {
47 setRandomTolerance();
48 }
49
50 const Route & route = m_track->trackData().route();
51 steerControl(route.get(m_race->getCurrentTargetNodeIndex(m_car.index())));
52
53 speedControl(*m_track->trackTileAtLocation(m_car.location().i(), m_car.location().j()), isRaceCompleted);
54
55 m_lastTargetNodeIndex = m_race->getCurrentTargetNodeIndex(m_car.index());
56 }
57 }
58
59 void AI::setRandomTolerance()
60 {
61 m_randomTolerance = MCRandom::randomVector2d() * TrackTileBase::width() / 8;
62 }
63
64 void AI::steerControl(TargetNodeBasePtr targetNode)
65 {
66 // Initial target coordinates
67 MCVector3dF target(static_cast<float>(targetNode->location().x()), static_cast<float>(targetNode->location().y()));
68 target -= MCVector3dF(m_car.location() + MCVector3dF(m_randomTolerance));
69
70 const float angle = MCTrigonom::radToDeg(std::atan2(target.j(), target.i()));
71 const float cur = static_cast<int>(m_car.angle()) % 360;
72 float diff = angle - cur;
73
74 bool ok = false;
75 while (!ok)
76 {
77 if (diff > 180)
78 {
79 diff = diff - 360;
80 ok = false;
81 }
82 else if (diff < -180)
83 {
84 diff = diff + 360;
85 ok = false;
86 }
87 else
88 {
89 ok = true;
90 }
91 }
92
93 // PID-controller. This makes the computer players to turn and react faster
94 // than the human player, but hey...they are stupid.
95 float control = diff * 0.025f + (diff - m_lastDiff) * 0.025f;
96 const float maxControl = 1.5;
97 control = control < 0 ? -control : control;
98 control = control > maxControl ? maxControl : control;
99
100 const float maxDelta = 3.0;
101 if (diff < -maxDelta)
102 {
103 m_car.steer(Car::Steer::Right, control);
104 }
105 else if (diff > maxDelta)
106 {
107 m_car.steer(Car::Steer::Left, control);
108 }
109
110 // Store the last difference
111 m_lastDiff = diff;
112 }
113
114 void AI::speedControl(TrackTile & currentTile, bool isRaceCompleted)
115 {
116 // TODO: Maybe it'd be possible to adjust speed according to
117 // the difference between current and target angles so that
118 // computer hints wouldn't be needed anymore..?
119
120 // Braking / acceleration logic
121 bool accelerate = true;
122 bool brake = false;
123
124 const float absSpeed = m_car.absSpeed();
125
126 // The following speed limits are experimentally defined.
127 float scale = 0.9f;
128 if (currentTile.computerHint() == TrackTile::ComputerHint::Brake)
129 {
130 if (absSpeed > 14.0f * scale)
131 {
132 brake = true;
133 }
134 }
135
136 if (currentTile.computerHint() == TrackTile::ComputerHint::BrakeHard)
137 {
138 if (absSpeed > 9.5f * scale)
139 {
140 brake = true;
141 }
142 }
143
144 if (currentTile.tileTypeEnum() == TrackTile::TileType::Corner90)
145 {
146 if (absSpeed > 7.0f * scale)
147 {
148 accelerate = false;
149 }
150 }
151
152 if (currentTile.tileTypeEnum() == TrackTile::TileType::Corner45Left || currentTile.tileTypeEnum() == TrackTile::TileType::Corner45Right)
153 {
154 if (absSpeed > 8.3f * scale)
155 {
156 accelerate = false;
157 }
158 }
159
160 if (isRaceCompleted)
161 {
162 // Cool down lap speed (should be greater than tire spin threshold)
163 if (absSpeed > 5.0f)
164 {
165 accelerate = false;
166 }
167 }
168 else
169 {
170 if (absSpeed < 3.6f * scale)
171 {
172 accelerate = true;
173 brake = false;
174 }
175 }
176
177 if (brake)
178 {
179 m_car.setAcceleratorEnabled(false);
180 m_car.setBrakeEnabled(true);
181 }
182 else if (accelerate)
183 {
184 m_car.setAcceleratorEnabled(true);
185 m_car.setBrakeEnabled(false);
186 }
187 else
188 {
189 m_car.setAcceleratorEnabled(false);
190 m_car.setBrakeEnabled(false);
191 }
192 }
193
194 void AI::setTrack(std::shared_ptr<Track> track)
195 {
196 m_track = track;
197 }