Skip to content

Commit 106515c

Browse files
committed
timeout feature
1 parent da34e51 commit 106515c

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

engine/src/main/battlecode/common/GameConstants.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,16 @@ public class GameConstants {
8585
/** The percent of the map which a team needs to paint to win. */
8686
public static final int PAINT_PERCENT_TO_WIN = 70;
8787

88-
/** The maximum number of towers that a team can have. */
89-
public static final int MAX_NUMBER_OF_TOWERS = 25;
88+
/** The maximum number of towers that a team can have. */
89+
public static final int MAX_NUMBER_OF_TOWERS = 25;
90+
91+
/**
92+
* The maximum execution time that can be spent on a team in one match. If the total time spent executing a team's bots
93+
* exceeds this limit, the team will immediately lose the game. Execution time is measured in ns.
94+
*/
95+
// public static final long MAX_TEAM_EXECUTION_TIME = 1200000000000L;
96+
97+
public static final long MAX_TEAM_EXECUTION_TIME = 5000000000L;
9098

9199
// *********************************
92100
// ****** GAME MECHANICS ***********

engine/src/main/battlecode/world/control/PlayerControlProvider.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package battlecode.world.control;
22

3+
import battlecode.common.GameConstants;
34
import battlecode.common.Team;
45
import battlecode.instrumenter.InstrumentationException;
56
import battlecode.instrumenter.TeamClassLoaderFactory;
@@ -70,6 +71,11 @@ public class PlayerControlProvider implements RobotControlProvider {
7071
*/
7172
private int matchId = -1;
7273

74+
/**
75+
* The total time the player's bots have spent executing, measured in ns
76+
*/
77+
private long totalPlayerTime = 0;
78+
7379
/**
7480
* Create a new PlayerControlProvider.
7581
*
@@ -116,6 +122,7 @@ public void matchEnded() {
116122

117123
this.sandboxes.clear();
118124
this.gameWorld = null;
125+
this.totalPlayerTime = 0;
119126
}
120127

121128
@Override
@@ -173,7 +180,12 @@ public void runRobot(InternalRobot robot) {
173180

174181
if (player != null) {
175182
player.setBytecodeLimit(robot.getBytecodeLimit());
183+
long timeBefore = System.nanoTime();
176184
player.step();
185+
totalPlayerTime += (System.nanoTime() - timeBefore);
186+
if(totalPlayerTime > GameConstants.MAX_TEAM_EXECUTION_TIME) {
187+
robot.getController().resign();
188+
}
177189
}
178190
}
179191

0 commit comments

Comments
 (0)