forked from Plankton555/SSCAIT-ObserverModule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CameraModule.cpp
408 lines (361 loc) · 9.07 KB
/
CameraModule.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include "CameraModule.h"
using namespace BWAPI;
CameraModule::CameraModule() : combatAcc(0), vision(0)
{
cameraMoveTime = std::chrono::seconds(6);
cameraMoveTimeMin = std::chrono::seconds(2);
watchScoutWorkerUntil = 7500;
lastMoved = clock.now();
lastMovedPriority = 0;
lastMovedPosition = BWAPI::Position(0, 0);
cameraFocusPosition = BWAPI::Position(0, 0);
cameraFocusUnit = NULL;
followUnit = false;
lastUnitDestroyedFrame = 2 * 60 * 24;
}
void CameraModule::onStart(BWAPI::Position startPos, int screenWidth, int screenHeight)
{
cameraFocusPosition = startPos;
currentCameraPosition = startPos;
scrWidth = screenWidth;
scrHeight = screenHeight;
if (Broodwar->getReplayFrameCount() < 2 * 60 * 24)
{
Broodwar->leaveGame();
}
}
void CameraModule::onFrame()
{
if (combatAcc > 0)
combatAcc--;
auto seconds = Broodwar->getFrameCount() * 42 / 1000;
auto minutes = seconds / 60;
seconds %= 60;
Broodwar->setTextSize(Text::Size::Huge);
Broodwar->drawTextScreen(Position(0, 0), "%c%c%02d:%02d", Text::Align_Right, Text::White, minutes, seconds);
// Broodwar->drawTextScreen(Position(10, 30), "%d", combatAcc);
slowDownOnCombat();
moveCameraFallingNuke();
moveCameraIsUnderAttack();
moveCameraIsAttacking();
if (Broodwar->getFrameCount() <= watchScoutWorkerUntil)
{
moveCameraScoutWorker();
}
moveCameraArmy();
moveCameraDrop();
updateCameraPosition();
updateGameSpeed();
updateVision();
}
bool CameraModule::isNearStartLocation(BWAPI::Player player, BWAPI::Position pos)
{
int distance = 1000;
BWAPI::Point<int, 32>::list startLocations = Broodwar->getStartLocations();
for (BWAPI::Point<int, 32>::list::iterator it = startLocations.begin(); it != startLocations.end(); it++)
{
Position startLocation = BWAPI::Position(*it);
// if the start position is not our own home, and the start position is closer than distance
if (!isNearOwnStartLocation(player, startLocation) && startLocation.getDistance(pos) <= distance)
{
return true;
}
}
return false;
}
bool CameraModule::isNearOwnStartLocation(BWAPI::Player player, BWAPI::Position pos)
{
int distance = 10 * TILE_SIZE; // 10*32
return (Position(player->getStartLocation()).getDistance(pos) <= distance);
}
bool CameraModule::isArmyUnit(BWAPI::Unit unit)
{
return !unit->getPlayer()->isNeutral() && unit->getType().supplyRequired() > 0 && !unit->getType().isWorker();
}
bool CameraModule::shouldMoveCamera(int priority)
{
auto delta = clock.now() - lastMoved;
bool isTimeToMove = delta >= cameraMoveTime;
bool isTimeToMoveIfHigherPrio = delta >= cameraMoveTimeMin;
bool isHigherPrio = lastMovedPriority < priority;
// camera should move IF: enough time has passed OR (minimum time has passed AND new prio is higher)
return isTimeToMove || (isHigherPrio && isTimeToMoveIfHigherPrio);
}
void CameraModule::slowDownOnCombat()
{
for (BWAPI::Unit unit : BWAPI::Broodwar->getAllUnits())
{
if (unit->isUnderAttack() || unit->isAttacking())
{
combatAcc += 2;
if (combatAcc >= 24 * 5)
{
combatAcc = 24 * 13;
}
return;
}
}
}
void CameraModule::moveCameraIsUnderAttack()
{
int prio = 3;
if (!shouldMoveCamera(prio))
{
return;
}
for (BWAPI::Unit unit : BWAPI::Broodwar->getAllUnits())
{
if (unit->isUnderAttack())
{
updateVision(unit, prio);
moveCamera(unit, prio);
return;
}
}
}
void CameraModule::moveCameraIsAttacking()
{
int prio = 3;
if (!shouldMoveCamera(prio))
{
return;
}
for (BWAPI::Unit unit : BWAPI::Broodwar->getAllUnits())
{
if (unit->isAttacking())
{
updateVision(unit, prio);
moveCamera(unit, prio);
return;
}
}
}
void CameraModule::moveCameraFallingNuke()
{
int prio = 5;
if (!shouldMoveCamera(prio))
{
return;
}
for (BWAPI::Unit unit : BWAPI::Broodwar->getAllUnits())
{
if (unit->getType() == UnitTypes::Terran_Nuclear_Missile && unit->getVelocityY() > 0)
{
moveCamera(unit, prio);
return;
}
}
}
void CameraModule::moveCameraScoutWorker()
{
int highPrio = 2;
int lowPrio = 0;
if (!shouldMoveCamera(lowPrio))
{
return;
}
for (BWAPI::Unit unit : BWAPI::Broodwar->getAllUnits())
{
if (!unit->getType().isWorker())
{
continue;
}
if (isNearStartLocation(unit->getPlayer(), unit->getPosition()))
{
updateVision(unit, highPrio);
moveCamera(unit, highPrio);
return;
}
else if (!isNearOwnStartLocation(unit->getPlayer(), unit->getPosition()))
{
updateVision(unit, lowPrio);
moveCamera(unit, lowPrio);
}
}
}
void CameraModule::moveCameraNukeDetect(BWAPI::Position target)
{
int prio = 4;
if (!shouldMoveCamera(prio))
{
return;
}
moveCamera(target, prio);
}
void CameraModule::moveCameraDrop()
{
int prio = 2;
if (!shouldMoveCamera(prio))
{
return;
}
for (BWAPI::Unit unit : BWAPI::Broodwar->getAllUnits())
{
if ((unit->getType() == UnitTypes::Zerg_Overlord || unit->getType() == UnitTypes::Terran_Dropship || unit->getType() == UnitTypes::Protoss_Shuttle) && isNearStartLocation(unit->getPlayer(), unit->getPosition()) && unit->getLoadedUnits().size() > 0)
{
updateVision(unit, prio);
moveCamera(unit, prio);
return;
}
}
}
void CameraModule::moveCameraArmy()
{
int prio = 1;
if (!shouldMoveCamera(prio))
{
return;
}
// Double loop, check if army units are close to each other
int radius = 50;
BWAPI::Position bestPos;
BWAPI::Unit bestPosUnit;
int mostUnitsNearby = 0;
for (BWAPI::Unit unit1 : BWAPI::Broodwar->getAllUnits())
{
if (!isArmyUnit(unit1))
{
continue;
}
BWAPI::Position uPos = unit1->getPosition();
int nrUnitsNearby = 0;
for (BWAPI::Unit unit2 : BWAPI::Broodwar->getUnitsInRadius(uPos, radius))
{
if (!isArmyUnit(unit2))
{
continue;
}
nrUnitsNearby++;
}
if (nrUnitsNearby > mostUnitsNearby)
{
mostUnitsNearby = nrUnitsNearby;
bestPos = uPos;
bestPosUnit = unit1;
}
}
if (mostUnitsNearby > 1)
{
updateVision(bestPosUnit, prio);
moveCamera(bestPosUnit, prio);
}
}
void CameraModule::moveCameraUnitCreated(BWAPI::Unit unit)
{
int prio = 1;
if (!shouldMoveCamera(prio))
{
return;
}
else if (unit->getPlayer() == Broodwar->self() && !unit->getType().isWorker())
{
moveCamera(unit, prio);
}
}
void CameraModule::moveCamera(BWAPI::Position pos, int priority)
{
if (!shouldMoveCamera(priority))
{
return;
}
if (followUnit == false && cameraFocusPosition == pos)
{
// don't register a camera move if the position is the same
return;
}
cameraFocusPosition = pos;
lastMovedPosition = cameraFocusPosition;
lastMoved = clock.now();
lastMovedPriority = priority;
followUnit = false;
}
void CameraModule::moveCamera(BWAPI::Unit unit, int priority)
{
if (!shouldMoveCamera(priority))
{
return;
}
if (followUnit == true && cameraFocusUnit == unit)
{
// don't register a camera move if we follow the same unit
return;
}
cameraFocusUnit = unit;
lastMovedPosition = cameraFocusUnit->getPosition();
lastMoved = clock.now();
lastMovedPriority = priority;
followUnit = true;
}
void CameraModule::updateCameraPosition()
{
double moveFactor = 0.1;
if (followUnit && cameraFocusUnit->getPosition().isValid())
{
cameraFocusPosition = cameraFocusUnit->getPosition();
}
currentCameraPosition = currentCameraPosition + BWAPI::Position(
(int)(moveFactor * (cameraFocusPosition.x - currentCameraPosition.x)),
(int)(moveFactor * (cameraFocusPosition.y - currentCameraPosition.y)));
BWAPI::Position currentMovedPosition =
currentCameraPosition - BWAPI::Position(scrWidth / 2, scrHeight / 2 - 40); // -40 to account for HUD
if (currentCameraPosition.isValid())
{
BWAPI::Broodwar->setScreenPosition(currentMovedPosition);
}
}
void CameraModule::onUnitDestroy(BWAPI::Unit unit)
{
lastUnitDestroyedFrame = Broodwar->getFrameCount();
}
void CameraModule::updateGameSpeed()
{
if (Broodwar->getFrameCount() < 30 * 24 || Broodwar->getFrameCount() > lastUnitDestroyedFrame + 5 * 60 * 24)
localSpeed = 5;
else if (lastMovedPriority == 0 || combatAcc <= 24 * 3)
localSpeed = 12;
else if (lastMovedPriority == 1)
localSpeed = 22;
else if (lastMovedPriority == 2)
localSpeed = 32;
else if (lastMovedPriority == 5)
localSpeed = 84;
else
localSpeed = 42;
Broodwar->setLocalSpeed(localSpeed);
if (localSpeed < 42 && (localSpeed >= 22 || std::chrono::duration_cast<std::chrono::seconds>(clock.now().time_since_epoch()).count() % 2 == 0))
{
Broodwar->setTextSize(Text::Size::Huge);
auto speed = ((84 + localSpeed / 2) / localSpeed) / 2.0;
Broodwar->drawTextScreen(Position(6, 0), "%cx%.1f", Text::Orange, speed);
Broodwar->drawTextScreen(Position(8, 0), "%cx%.1f", Text::Yellow, speed);
Broodwar->setTextSize();
}
}
bool CameraModule::shouldUpdateVision(int priority)
{
return shouldMoveCamera(priority);
}
void CameraModule::updateVision(BWAPI::Unit unit, int priority)
{
updateVision(unit->getPlayer(), priority);
}
void CameraModule::updateVision(BWAPI::Player player, int priority)
{
if (!shouldUpdateVision(priority))
{
return;
}
vision = 1 << player->getID();
}
void CameraModule::updateVision()
{
if (shouldUpdateVision(0))
{
vision = 0;
}
// Broodwar << vision << std::endl;
for (auto player : Broodwar->getPlayers())
{
Broodwar->setVision(player, vision[player->getID()]);
}
}