-
Notifications
You must be signed in to change notification settings - Fork 271
/
GameImpl.cpp
executable file
·908 lines (848 loc) · 32.4 KB
/
GameImpl.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
#include "../../../svnrev.h"
#include "GameImpl.h"
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <fstream>
#include <Util/Clamp.h>
#include <Util/Convenience.h>
#include <BWAPI/ForceImpl.h>
#include <BWAPI/PlayerImpl.h>
#include <BWAPI/UnitImpl.h>
#include <BWAPI/BulletImpl.h>
#include <Templates.h>
#include <BWAPI/Command.h>
#include <BWAPI/Map.h>
#include <BWAPI/Flag.h>
#include <BWAPI/UnaryFilter.h>
#include <BWAPI/Unitset.h>
#include <BW/CUnit.h>
#include <BW/CBullet.h>
#include <BW/Offsets.h>
#include <BW/UnitTarget.h>
#include <BW/OrderTypes.h>
#include <BWAPI/Latency.h>
#include <BW/TileType.h>
#include <BW/TileSet.h>
#include <BW/CheatType.h>
#include <BW/Dialog.h>
#include "BWAPI/AIModule.h"
#include "../DLLMain.h"
#include "../Config.h"
#include "../Detours.h"
#include "../NewHackUtil.h"
#include "BWtoBWAPI.h"
#include "../BW/Pathing.h"
#include "../../../Debug.h"
namespace BWAPI
{
//----------------------------------------------------------------------------------------------------------
Force GameImpl::getForce(int forceID) const
{
return server.getForce(forceID);
}
//----------------------------------------------------------------------------------------------------------
Region GameImpl::getRegion(int regionID) const
{
auto it = regionMap.find(regionID);
return it != regionMap.end() ? it->second : nullptr;
}
//----------------------------------------------------------------------------------------------------------
Player GameImpl::getPlayer(int playerID) const
{
return server.getPlayer(playerID);
}
//----------------------------------------------------------------------------------------------------------
Unit GameImpl::getUnit(int unitID) const
{
return server.getUnit(unitID);
}
//----------------------------------------------------------------------------------------------------------
Unit GameImpl::indexToUnit(int unitIndex) const
{
if ( !this->isFlagEnabled(Flag::CompleteMapInformation) )
return nullptr;
int i = (unitIndex & 0x7FF);
if (i < BW::UNIT_ARRAY_MAX_LENGTH && this->unitArray[i]->canAccess())
return this->unitArray[i];
return nullptr;
}
//--------------------------------------------- GET GAME TYPE ----------------------------------------------
GameType GameImpl::getGameType() const
{
if (isReplay())
return GameTypes::None;
return GameType(BW::BWDATA::gameType);
}
//---------------------------------------------- GET LATENCY -----------------------------------------------
int GameImpl::getLatency() const
{
// Returns the real latency values
if ( !this->isMultiplayer() )
return BWAPI::Latency::SinglePlayer;
if ( this->isBattleNet() )
{
switch(BW::BWDATA::Latency)
{
case 0:
return BWAPI::Latency::BattlenetLow;
case 1:
return BWAPI::Latency::BattlenetMedium;
case 2:
return BWAPI::Latency::BattlenetHigh;
default:
return BWAPI::Latency::BattlenetLow;
}
}
else
{
switch(BW::BWDATA::Latency)
{
case 0:
return BWAPI::Latency::LanLow;
case 1:
return BWAPI::Latency::LanMedium;
case 2:
return BWAPI::Latency::LanHigh;
default:
return BWAPI::Latency::LanLow;
}
}
}
//--------------------------------------------- GET FRAME COUNT --------------------------------------------
int GameImpl::getFrameCount() const
{
return this->frameCount;
}
//--------------------------------------------- GET REPLAY FRAME COUNT -------------------------------------
int GameImpl::getReplayFrameCount() const
{
return BW::BWDATA::ReplayHead.frameCount;
}
//------------------------------------------------ GET FPS -------------------------------------------------
int GameImpl::getFPS() const
{
return fpsCounter.getFps();
}
//-------------------------------------------- GET Average FPS ---------------------------------------------
double GameImpl::getAverageFPS() const
{
return fpsCounter.getAverageFps();
}
//------------------------------------------- GET MOUSE POSITION -------------------------------------------
BWAPI::Position GameImpl::getMousePosition() const
{
if ( !this->isFlagEnabled(BWAPI::Flag::UserInput) )
return BWAPI::Positions::Unknown;
return BWAPI::Position(BW::BWDATA::Mouse.x, BW::BWDATA::Mouse.y);
}
//--------------------------------------------- GET MOUSE STATE --------------------------------------------
bool GameImpl::getMouseState(MouseButton button) const
{
if ( !this->isFlagEnabled(BWAPI::Flag::UserInput) )
return false;
int vkValue;
switch ( button )
{
case BWAPI::M_LEFT:
vkValue = VK_LBUTTON;
break;
case BWAPI::M_RIGHT:
vkValue = VK_RBUTTON;
break;
case BWAPI::M_MIDDLE:
vkValue = VK_MBUTTON;
break;
default:
return false;
}
return (GetKeyState(vkValue) & 0x80) > 0;
}
//---------------------------------------------- GET KEY STATE ---------------------------------------------
bool GameImpl::getKeyState(Key key) const
{
if ( !this->isFlagEnabled(BWAPI::Flag::UserInput) )
return false;
if ( key < 0 || key >= K_MAX )
return false;
return (GetKeyState(key) & 128) > 0;
}
//------------------------------------------- GET SCREEN POSITION ------------------------------------------
BWAPI::Position GameImpl::getScreenPosition() const
{
if ( !this->isFlagEnabled(BWAPI::Flag::UserInput) )
return BWAPI::Positions::Unknown;
return BWAPI::Position(BW::BWDATA::ScreenX, BW::BWDATA::ScreenY);
}
//------------------------------------------- SET SCREEN POSITION ------------------------------------------
void GameImpl::setScreenPosition(int x, int y)
{
this->setLastError();
if ( !data->hasGUI ) return;
Position scrSize(BW::BWDATA::GameScreenBuffer.width(), BW::BWDATA::GameScreenBuffer.height()-108);
Position mapSize( TilePosition(Map::getWidth(), Map::getHeight()) );
// Sets the screen's position relative to the map
Position movePos(x,y);
movePos.setMin(0, 0);
movePos.setMax( mapSize - scrSize);
movePos &= 0xFFFFFFF8;
BW::BWDATA::MoveToX = movePos.x;
BW::BWDATA::MoveToY = movePos.y;
BW::BWDATA::Game.screenTilePosition = BW::TilePosition(movePos);
BW::BWFXN_UpdateScreenPosition();
}
//---------------------------------------------- PING MINIMAP ----------------------------------------------
void GameImpl::pingMinimap(int x, int y)
{
this->setLastError();
QUEUE_COMMAND(BW::Orders::MinimapPing, x, y);
}
//--------------------------------------------- IS FLAG ENABLED --------------------------------------------
bool GameImpl::isFlagEnabled(int flag) const
{
// Check if index is valid
if ( flag < 0 || flag >= BWAPI::Flag::Max )
return false;
// Make completeMapInfo appear true if the match has ended
if ( flag == Flag::CompleteMapInformation && this->calledMatchEnd )
return true;
// Return the state of the flag
return this->flags[flag];
}
//----------------------------------------------- ENABLE FLAG ----------------------------------------------
void GameImpl::enableFlag(int flag)
{
// Enable the specified flag
this->setLastError();
// Don't allow flag changes after frame 0
if ( this->frameCount > 0 )
{
this->setLastError(Errors::Access_Denied);
return;
}
// check if index is valid
if ( flag < 0 || flag >= BWAPI::Flag::Max )
{
this->setLastError(Errors::Invalid_Parameter);
return;
}
// check if tournament will allow the call
if ( !this->tournamentCheck(Tournament::EnableFlag, &flag) )
return;
// Modify flag state
this->flags[flag] = true;
if ( !this->hTournamentModule )
{
switch(flag)
{
case BWAPI::Flag::CompleteMapInformation:
this->sendText("Enabled Flag CompleteMapInformation");
break;
case BWAPI::Flag::UserInput:
this->sendText("Enabled Flag UserInput");
break;
}
}
}
//--------------------------------------------- GET UNITS IN RECTANGLE -------------------------------------
Unit GameImpl::_unitFromIndex(int index)
{
return this->unitArray[index-1];
}
Unitset GameImpl::getUnitsInRectangle(int left, int top, int right, int bottom, const UnitFilter &pred) const
{
Unitset unitFinderResults;
// Have the unit finder do its stuff
Templates::iterateUnitFinder<BW::unitFinder>(BW::BWDATA::UnitOrderingX.data(),
BW::BWDATA::UnitOrderingY.data(),
BW::BWDATA::UnitOrderingCount,
left,
top,
right,
bottom,
[&](Unit u){ if ( !pred.isValid() || pred(u) )
unitFinderResults.insert(u); });
// Return results
return unitFinderResults;
}
Unit GameImpl::getClosestUnitInRectangle(Position center, const UnitFilter &pred, int left, int top, int right, int bottom) const
{
// cppcheck-suppress variableScope
int bestDistance = 99999999;
Unit pBestUnit = nullptr;
Templates::iterateUnitFinder<BW::unitFinder>( BW::BWDATA::UnitOrderingX.data(),
BW::BWDATA::UnitOrderingY.data(),
BW::BWDATA::UnitOrderingCount,
left,
top,
right,
bottom,
[&](Unit u){ if ( !pred.isValid() || pred(u) )
{
int newDistance = u->getDistance(center);
if ( newDistance < bestDistance )
{
pBestUnit = u;
bestDistance = newDistance;
}
} } );
return pBestUnit;
}
Unit GameImpl::getBestUnit(const BestUnitFilter &best, const UnitFilter &pred, Position center, int radius) const
{
Unit pBestUnit = nullptr;
Position rad(radius,radius);
Position topLeft(center - rad);
Position botRight(center + rad);
topLeft.makeValid();
botRight.makeValid();
Templates::iterateUnitFinder<BW::unitFinder>( BW::BWDATA::UnitOrderingX.data(),
BW::BWDATA::UnitOrderingY.data(),
BW::BWDATA::UnitOrderingCount,
topLeft.x,
topLeft.y,
botRight.x,
botRight.y,
[&](Unit u){ if ( !pred.isValid() || pred(u) )
{
if ( pBestUnit == nullptr )
pBestUnit = u;
else
pBestUnit = best(pBestUnit,u);
} } );
return pBestUnit;
}
//----------------------------------------------- MAP WIDTH ------------------------------------------------
int GameImpl::mapWidth() const
{
return Map::getWidth();
}
//----------------------------------------------- MAP HEIGHT -----------------------------------------------
int GameImpl::mapHeight() const
{
return Map::getHeight();
}
//---------------------------------------------- MAP FILE NAME ---------------------------------------------
std::string GameImpl::mapFileName() const
{
return Map::getFileName();
}
//---------------------------------------------- MAP PATH NAME ---------------------------------------------
std::string GameImpl::mapPathName() const
{
return Map::getPathName();
}
//------------------------------------------------ MAP NAME ------------------------------------------------
std::string GameImpl::mapName() const
{
return Map::getName();
}
//----------------------------------------------- GET MAP HASH ---------------------------------------------
std::string GameImpl::mapHash() const
{
return Map::getMapHash();
}
//--------------------------------------------- IS WALKABLE ------------------------------------------------
bool GameImpl::isWalkable(int x, int y) const
{
return Map::walkable(x, y);
}
//--------------------------------------------- GET GROUND HEIGHT ------------------------------------------
int GameImpl::getGroundHeight(int x, int y) const
{
return Map::groundHeight(x, y);
}
//--------------------------------------------- IS BUILDABLE -----------------------------------------------
bool GameImpl::isBuildable(int x, int y, bool includeBuildings) const
{
if ( Map::buildable(x,y) )
{
if ( includeBuildings && this->isVisible(x,y) && Map::isOccupied(x,y) )
return false;
return true;
}
return false;
}
//--------------------------------------------- IS VISIBLE -------------------------------------------------
bool GameImpl::isVisible(int x, int y) const
{
return Map::visible(x, y);
}
//--------------------------------------------- IS EXPLORED ------------------------------------------------
bool GameImpl::isExplored(int x, int y) const
{
return Map::isExplored(x, y);
}
//--------------------------------------------- HAS CREEP --------------------------------------------------
bool GameImpl::hasCreep(int x, int y) const
{
if (!this->isFlagEnabled(Flag::CompleteMapInformation) && !this->isVisible(x, y))
return false;
return Map::hasCreep(x, y);
}
//--------------------------------------------- HAS POWER --------------------------------------------------
bool GameImpl::hasPowerPrecise(int x, int y, UnitType unitType) const
{
return Templates::hasPower(x, y, unitType, pylons);
}
//------------------------------------------------- PRINTF -------------------------------------------------
void GameImpl::vPrintf(const char *format, va_list arg)
{
// nogui & safety
if ( !data->hasGUI ) return;
if ( !format ) return;
// Expand format into buffer
char buffer[512];
VSNPrintf(buffer, format, arg);
if ( !this->tournamentCheck(Tournament::Printf, buffer) )
return;
// Dispatch message using existing Storm library function (lobby+game)
S_EVT evt = { 4, -1, buffer, strlen(buffer) + 1 };
SEvtDispatch('SNET', 1, 4, &evt);
}
//--------------------------------------------- SEND TEXT --------------------------------------------------
void GameImpl::vSendTextEx(bool toAllies, const char *format, va_list arg)
{
// safety
if ( !format ) return;
// Expand format and store in buffer
char buffer[80]; // Use maximum size of 80 since there is a hardcoded limit in Broodwar of 80 characters
VSNPrintf(buffer, format, arg);
// Check if tournament module allows sending text
if ( !this->tournamentCheck(Tournament::SendText, buffer) )
return;
if ( buffer[0] == '/' ) // If we expect a battle.net command
{
SNetSendServerChatCommand(buffer);
return;
}
if ( this->isReplay() ) // Just print the text if in a replay
{
Broodwar << buffer << std::endl;
return;
}
// If we're in single player
if ( this->isInGame() && !this->isMultiplayer() )
{
BW::CheatFlags::Enum cheatID = BW::getCheatFlag(buffer);
if ( cheatID != BW::CheatFlags::None ) // Apply cheat code if it is one
{
this->cheatFlags ^= cheatID;
QUEUE_COMMAND(BW::Orders::UseCheat, this->cheatFlags);
if (cheatID == BW::CheatFlags::ShowMeTheMoney ||
cheatID == BW::CheatFlags::BreatheDeep ||
cheatID == BW::CheatFlags::WhatsMineIsMine ||
cheatID == BW::CheatFlags::SomethingForNothing)
this->cheatFlags ^= cheatID;
}
else // Just print the message otherwise
{
Broodwar << this->BWAPIPlayer->getTextColor() << this->BWAPIPlayer->getName()
<< ": " << Text::Green << buffer << std::endl;
}
} // single
else // multiplayer or lobby
{
// Otherwise, send the message using Storm command
char szMessage[82];
szMessage[0] = 0;
szMessage[1] = 1;
int msgLen = SStrCopy(&szMessage[2], buffer, 80);
if ( this->isInGame() ) // in game
{
if ( toAllies ) // Send to all allies
{
for (int i = 0; i < BW::PLAYABLE_PLAYER_COUNT; ++i)
if ( this->BWAPIPlayer->isAlly(players[i]) && BW::BWDATA::Players[i].dwStormId != -1 )
SNetSendMessage(BW::BWDATA::Players[i].dwStormId, szMessage, msgLen + 3);
}
else // Otherwise send to all
{
SNetSendMessage(-1, szMessage, msgLen + 3);
} // toAllies
}
else // assume in lobby, then send lobby message
{
szMessage[1] = 0x4C;
SNetSendMessage(-1, &szMessage[1], msgLen + 2);
} // isInGame
} // multi
}
//------------------------------------------------ IS IN GAME ----------------------------------------------
bool GameImpl::isInGame() const
{
return inGame;
}
//-------------------------------------------- IS SINGLE PLAYER --------------------------------------------
bool GameImpl::isMultiplayer() const
{
return BW::BWDATA::NetMode != 0 && BW::BWDATA::NetMode != -1;
}
//--------------------------------------------- IS BATTLE NET ----------------------------------------------
bool GameImpl::isBattleNet() const
{
return BW::BWDATA::NetMode == 'BNET';
}
//----------------------------------------------- IS PAUSED ------------------------------------------------
bool GameImpl::isPaused() const
{
return BW::BWDATA::isGamePaused != 0;
}
//----------------------------------------------- IN REPLAY ------------------------------------------------
bool GameImpl::isReplay() const
{
return BW::BWDATA::InReplay != 0;
}
//----------------------------------------------- START GAME -----------------------------------------------
void GameImpl::_startGame()
{
// Starts the game as a lobby host
QUEUE_COMMAND(BW::Orders::StartGame);
}
//----------------------------------------------- PAUSE GAME -----------------------------------------------
void GameImpl::pauseGame()
{
// Pauses the game
this->setLastError();
if ( !this->tournamentCheck(Tournament::PauseGame) )
return;
QUEUE_COMMAND(BW::Orders::PauseGame);
}
//---------------------------------------------- RESUME GAME -----------------------------------------------
void GameImpl::resumeGame()
{
// Resumes the game
this->setLastError();
if ( !this->tournamentCheck(Tournament::ResumeGame) )
return;
QUEUE_COMMAND(BW::Orders::ResumeGame);
}
//---------------------------------------------- LEAVE GAME ------------------------------------------------
void GameImpl::leaveGame()
{
// Leaves the current game. Moves directly to the post-game score screen
this->setLastError();
if ( !this->tournamentCheck(Tournament::LeaveGame) )
return;
BW::BWDATA::GameState = 0;
BW::BWDATA::gwNextGameMode = 6;
}
//--------------------------------------------- RESTART GAME -----------------------------------------------
void GameImpl::restartGame()
{
// Restarts the current match
// Does not work on Battle.net
if ( this->isMultiplayer() )
{
this->setLastError(Errors::Invalid_Parameter);
return;
}
this->setLastError();
QUEUE_COMMAND(BW::Orders::RestartGame);
}
//--------------------------------------------- SET ALLIANCE -----------------------------------------------
bool GameImpl::setAlliance(BWAPI::Player player, bool allied, bool alliedVictory)
{
// Set the current player's alliance status
if ( !BWAPIPlayer || isReplay() || !player || player == BWAPIPlayer )
return this->setLastError(Errors::Invalid_Parameter);
u32 alliance = 0;
for (int i = 0; i < BW::PLAYER_COUNT; ++i)
alliance |= (BW::BWDATA::Game.playerAlliances[BWAPIPlayer->getIndex()][i] & 3) << (i*2);
u8 newAlliance = allied ? (alliedVictory ? 2 : 1) : 0;
if ( allied )
alliance |= newAlliance << ( static_cast<PlayerImpl*>(player)->getIndex()*2);
else
alliance &= ~(3 << ( static_cast<PlayerImpl*>(player)->getIndex()*2) );
QUEUE_COMMAND(BW::Orders::SetAllies, alliance);
return this->setLastError();
}
//----------------------------------------------- SET VISION -----------------------------------------------
bool GameImpl::setVision(BWAPI::Player player, bool enabled)
{
if ( !player ) // Parameter check
return this->setLastError(Errors::Invalid_Parameter);
if ( this->isReplay() )
{
u32 vision = BW::BWDATA::ReplayVision;
if ( enabled )
vision |= 1 << static_cast<PlayerImpl*>(player)->getIndex();
else
vision &= ~(1 << static_cast<PlayerImpl*>(player)->getIndex() );
BW::BWDATA::ReplayVision = vision;
}
else
{
if ( !BWAPIPlayer || player == BWAPIPlayer )
return this->setLastError(Errors::Invalid_Parameter);
u16 vision = static_cast<u16>(BW::BWDATA::Game.playerVision[BWAPIPlayer->getIndex()]);
if ( enabled )
vision |= 1 << static_cast<PlayerImpl*>(player)->getIndex();
else
vision &= ~(1 << static_cast<PlayerImpl*>(player)->getIndex() );
QUEUE_COMMAND(BW::Orders::SetVision, vision);
}
return this->setLastError();
}
//--------------------------------------------------- GAME SPEED -------------------------------------------
void GameImpl::setLocalSpeed(int speed)
{
// Sets the frame rate of the client
if (!this->tournamentCheck(Tournament::SetLocalSpeed, &speed) ||
this->speedOverride != std::numeric_limits<decltype(this->speedOverride)>::min()) return;
setLocalSpeedDirect(speed);
}
void GameImpl::setLocalSpeedDirect(int speed)
{
if (speed < 0)
{
// Reset the speed if it is negative
for (int i = 0; i < BW::NUM_SPEEDS; ++i)
{
BW::BWDATA::GameSpeedModifiers.gameSpeedModifiers[i] = BW::OriginalSpeedModifiers[i];
BW::BWDATA::GameSpeedModifiers.altSpeedModifiers[i] = BW::OriginalSpeedModifiers[i] * 3;
}
}
else
{
// Set all speeds if it is positive
for (int i = 0; i < BW::NUM_SPEEDS; ++i)
{
BW::BWDATA::GameSpeedModifiers.gameSpeedModifiers[i] = speed;
BW::BWDATA::GameSpeedModifiers.altSpeedModifiers[i] = speed * 3;
}
}
}
void GameImpl::setFrameSkip(int frameSkip)
{
setLastError(Errors::None);
if ( !this->tournamentCheck(Tournament::SetFrameSkip, &frameSkip) )
return;
if ( frameSkip > 0 )
{
BW::BWDATA::FrameSkip = frameSkip;
return;
}
setLastError(Errors::Invalid_Parameter);
}
//------------------------------------------ ISSUE COMMAND -------------------------------------------------
bool GameImpl::issueCommand(const Unitset& units, UnitCommand command)
{
std::vector< std::vector<Unit> > groupsOf12;
std::vector<Unit> nextGroup;
// Iterate the set of units
for (Unit u : units)
{
// Skip on invalid units that can't issue commands
if ( !u->exists() )
continue;
// If unit can't issue the command while grouped (e.g. if it is a building) then try to issue
// it individually instead.
if ( !u->canIssueCommandGrouped(command) )
{
u->issueCommand(command);
continue;
}
// If the command optimizer has taken over the command, then don't add it to this group
if ( static_cast<UnitImpl*>(u)->prepareIssueCommand(command) )
continue;
// Insert the unit into the next group
nextGroup.push_back(u);
// Create a new group of 12
if ( nextGroup.size() >= 12 )
{
groupsOf12.push_back(nextGroup);
nextGroup.clear();
}
}
// Insert the last group into the groups of 12, if it is an incomplete group
if ( !nextGroup.empty() )
groupsOf12.push_back(nextGroup);
// Return if no units to command
if ( groupsOf12.empty() )
return false;
// Iterate our groups of 12
for ( auto &v : groupsOf12 )
{
// Get the first unit available
command.unit = v.front();
// Command optimization (no select) for unit unloading, but only if optimizer level >= 2
// TODO this piece should be extracted to the CommandOptimizer
if ( command.type != BWAPI::UnitCommandTypes::Unload || commandOptimizer.level < 2 )
{
// Select the unit group
BW::Orders::Select sel(v);
QueueGameCommand(&sel, sel.size());
apmCounter.addSelect();
}
// Execute the command
BroodwarImpl.executeCommand( command );
}
return true;
}
//------------------------------------------ GET SELECTED UNITS --------------------------------------------
const Unitset& GameImpl::getSelectedUnits() const
{
this->setLastError();
if ( !this->isFlagEnabled(BWAPI::Flag::UserInput) )
{
this->setLastError(Errors::Access_Denied);
return Unitset::none;
}
return selectedUnitSet;
}
//----------------------------------------------------- SELF -----------------------------------------------
Player GameImpl::self() const
{
return this->BWAPIPlayer;
}
//----------------------------------------------------- ENEMY ----------------------------------------------
Player GameImpl::enemy() const
{
return this->enemyPlayer;
}
//----------------------------------------------------- NEUTRAL --------------------------------------------
Player GameImpl::neutral() const
{
return players[11];
}
//----------------------------------------------------- ALLIES ---------------------------------------------
Playerset& GameImpl::allies()
{
return _allies;
}
//----------------------------------------------------- ENEMIES --------------------------------------------
Playerset& GameImpl::enemies()
{
return _enemies;
}
//---------------------------------------------------- OBSERVERS -------------------------------------------
Playerset& GameImpl::observers()
{
return _observers;
}
//--------------------------------------------------- LATENCY ----------------------------------------------
int GameImpl::getLatencyFrames() const
{
DWORD dwCallDelay = 1;
if ( BW::BWDATA::NetMode )
{
CAPS caps;
caps.dwSize = sizeof(CAPS);
SNetGetProviderCaps(&caps);
dwCallDelay = Util::clamp<int>(caps.dwCallDelay, 2, 8);
}
return (BW::BWDATA::LatencyFrames[BW::BWDATA::GameSpeed]) * (BW::BWDATA::Latency + dwCallDelay + 1);
}
int GameImpl::getLatencyTime() const
{
return getLatencyFrames() * BW::BWDATA::GameSpeedModifiers.gameSpeedModifiers[BW::BWDATA::GameSpeed];
}
int GameImpl::getRemainingLatencyFrames() const
{
return getLatencyFrames() - (this->getFrameCount() - lastTurnFrame);
}
int GameImpl::getRemainingLatencyTime() const
{
return (getLatencyFrames() * BW::BWDATA::GameSpeedModifiers.gameSpeedModifiers[BW::BWDATA::GameSpeed]) - (GetTickCount() - lastTurnTime);
}
//--------------------------------------------------- VERSION ----------------------------------------------
int GameImpl::getRevision() const
{
return SVN_REV;
}
int GameImpl::getClientVersion() const
{
return CLIENT_VERSION;
}
bool GameImpl::isDebug() const
{
return BUILD_DEBUG == 1;
}
//----------------------------------------------- LAT COMPENSATION -----------------------------------------
bool GameImpl::isLatComEnabled() const
{
return data->hasLatCom;
}
void GameImpl::setLatCom(bool isEnabled)
{
if ( !this->tournamentCheck(Tournament::SetLatCom, &isEnabled) )
return;
data->hasLatCom = isEnabled;
}
//----------------------------------------------- GET INSTANCE ID ------------------------------------------
int GameImpl::getInstanceNumber() const
{
return (int)gdwProcNum;
}
//---------------------------------------------------- GET APM ---------------------------------------------
int GameImpl::getAPM(bool includeSelects) const
{
return apmCounter.apm(includeSelects);
}
//---------------------------------------------------- SET MAP ---------------------------------------------
bool GameImpl::setMap(const char *mapFileName)
{
if ( !mapFileName || strlen(mapFileName) >= std::extent<decltype(BW::BWDATA::Game.mapFileName)>::value || !mapFileName[0] )
return setLastError(Errors::Invalid_Parameter);
if ( !std::ifstream(mapFileName).is_open() )
return setLastError(Errors::File_Not_Found);
if ( !this->tournamentCheck(Tournament::SetMap, (void*)mapFileName) )
return setLastError(Errors::None);
strcpy(BW::BWDATA::Game.mapFileName, mapFileName);
return setLastError(Errors::None);
}
//------------------------------------------------- ELAPSED TIME -------------------------------------------
int GameImpl::elapsedTime() const
{
return BW::BWDATA::Game.elapsedTime;
}
//-------------------------------------- SET COMMAND OPTIMIZATION LEVEL ------------------------------------
void GameImpl::setCommandOptimizationLevel(int level)
{
level = Util::clamp(level, 0, 4);
if ( !this->tournamentCheck(Tournament::SetCommandOptimizationLevel, &level) )
return;
this->commandOptimizer.level = level;
}
//----------------------------------------------- COUNTDOWN TIMER ------------------------------------------
int GameImpl::countdownTimer() const
{
return BW::BWDATA::Game.countdownTimer;
}
//------------------------------------------------- GET REGION AT ------------------------------------------
BWAPI::Region GameImpl::getRegionAt(int x, int y) const
{
this->setLastError();
if ( !Position(x, y) )
{
this->setLastError(BWAPI::Errors::Invalid_Parameter);
return nullptr;
}
const BW::region * const rgn = BW::getRegionAt(x,y);
if ( !rgn )
{
this->setLastError(BWAPI::Errors::Invalid_Parameter);
return nullptr;
}
return getRegion(rgn->getIndex());
}
int GameImpl::getLastEventTime() const
{
return this->lastEventTime;
}
bool GameImpl::setRevealAll(bool reveal)
{
if ( !isReplay() )
return this->setLastError(Errors::Invalid_Parameter);
BW::BWDATA::ReplayRevealAll = reveal ? 1 : 0;
return this->setLastError();
}
//-------------------------------------------------- GET RANDOM SEED ---------------------------------------
unsigned GameImpl::getRandomSeed() const
{
return BW::BWDATA::ReplayHead.gameSeed.randSeed;
}
};