Skip to content

Commit 8ffb7c4

Browse files
MLanghofkrisives
authored andcommitted
Added sensor and lazer interaction with bots. (#192)
Includes unit tests. Also added more tests for the sensor beam.
1 parent 546c206 commit 8ffb7c4

3 files changed

Lines changed: 120 additions & 2 deletions

File tree

src/asmcup/runtime/Robot.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ protected void tickMotor(World world) {
231231

232232
float tx = x + (float)StrictMath.cos(facing) * s;
233233
float ty = y + (float)StrictMath.sin(facing) * s;
234-
int radius = COLLIDE_RADIUS;
234+
int radius = COLLIDE_RANGE;
235235

236236
if (!world.isSolid(tx, ty, radius)) {
237237
x = tx;
@@ -284,6 +284,12 @@ protected void tickLazer(World world) {
284284
lazerEnd = i * RAY_INTERVAL;
285285
return;
286286
}
287+
288+
Robot robot = world.getRobot(tx, ty);
289+
if (robot != null && robot != this) {
290+
robot.damage(LAZER_DAMAGE);
291+
return;
292+
}
287293
}
288294

289295
lazerEnd = RAY_INTERVAL * RAY_STEPS;
@@ -422,6 +428,15 @@ protected int sensorPoint(World world, float sx, float sy) {
422428
}
423429
}
424430

431+
Robot robot = world.getRobot(sx, sy);
432+
433+
if ((sensorIgnore & SENSOR_ROBOT) == 0) {
434+
if (robot != null && robot != this) {
435+
// TODO: Add alliance once implemented!
436+
return SENSOR_ROBOT;
437+
}
438+
}
439+
425440
return 0;
426441
}
427442

@@ -470,6 +485,7 @@ protected static float floatModPositive(float dividend, float divisor) {
470485
public static final float FREQUENCY_MAX = 1000 * 10;
471486
public static final int LAZER_RANGE = 100;
472487
public static final int LAZER_BATTERY_COST = 4;
488+
public static final int LAZER_DAMAGE = 1024;
473489

474490
public static final int SENSOR_WALL = 1;
475491
public static final int SENSOR_HAZARD = 2;
@@ -482,5 +498,5 @@ protected static float floatModPositive(float dividend, float divisor) {
482498
public static final int RAY_STEPS = 64;
483499
public static final int RAY_RANGE = RAY_INTERVAL * RAY_STEPS;
484500

485-
public static final int COLLIDE_RADIUS = 10;
501+
public static final int COLLIDE_RANGE = 10;
486502
}

src/asmcup/runtime/World.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,16 @@ protected void tickItems(Robot robot) {
182182
item.collect(robot);
183183
cell.removeItem(item);
184184
}
185+
186+
public Robot getRobot(float x, float y) {
187+
for (Robot robot : robots) {
188+
if (Math.abs(robot.getX() - x) < Robot.COLLIDE_RANGE
189+
&& Math.abs(robot.getY() - y) < Robot.COLLIDE_RANGE) {
190+
return robot;
191+
}
192+
}
193+
return null;
194+
}
185195

186196
public void mark(Robot robot, int offset, int value) {
187197
int key = robot.getColumn() | (robot.getRow() << 16);

test/asmcup/runtime/RobotTest.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,17 @@ public void testIOBattery() {
139139
robot.handleIO(world);
140140
assertEquals(1.0f, vm.popFloat(), 0.0001f);
141141
}
142+
143+
@Test
144+
public void testIOLazer() {
145+
World world = new World();
146+
VM vm = robot.getVM();
147+
vm.pushFloat(1.0f);
148+
vm.push8(Robot.IO_LASER);
149+
vm.setIO(true);
150+
robot.handleIO(world);
151+
assertEquals(1.0f, robot.getLazer(), 0.0001f);
152+
}
142153

143154
@Test
144155
public void testIOCompass() {
@@ -213,6 +224,73 @@ public void testMarkOffset() {
213224
assertEquals(42, vm.pop8());
214225
}
215226

227+
@Test
228+
public void testSensorNothing() {
229+
World world = generateEmptyWorld((int) robot.getX(), (int) robot.getY(), Robot.RAY_RANGE + 10);
230+
world.addRobot(robot);
231+
VM vm = robot.getVM();
232+
233+
vm.push8(Robot.IO_SENSOR);
234+
vm.setIO(true);
235+
robot.handleIO(world);
236+
237+
assertEquals(vm.pop8() & 0b111111, 0);
238+
assertEquals(vm.popFloat(), Robot.RAY_RANGE, 5.0f);
239+
}
240+
241+
@Test
242+
public void testSensorWall() {
243+
testSensorHitTile(Cell.TILE_WALL, Robot.SENSOR_WALL);
244+
}
245+
246+
@Test
247+
public void testSensorObstacle() {
248+
testSensorHitTile(Cell.TILE_OBSTACLE, Robot.SENSOR_OBSTACLE);
249+
}
250+
251+
@Test
252+
public void testSensorHazard() {
253+
testSensorHitTile(Cell.TILE_HAZARD, Robot.SENSOR_HAZARD);
254+
}
255+
256+
protected void testSensorHitTile(int tile, int expectedSensorValue) {
257+
World world = generateEmptyWorld((int) robot.getX(), (int) robot.getY(), 100);
258+
world.addRobot(robot);
259+
VM vm = robot.getVM();
260+
float xOffset = 60.0f;
261+
world.setTileXY(robot.getX() + xOffset, robot.getY(), tile);
262+
263+
vm.push8(Robot.IO_SENSOR);
264+
vm.setIO(true);
265+
robot.handleIO(world);
266+
// Saw a wall...
267+
assertEquals(vm.pop8() & 0b111111, expectedSensorValue);
268+
// ...closeby.
269+
assertEquals(vm.popFloat(), xOffset, World.TILE_SIZE);
270+
}
271+
272+
@Test
273+
public void testSensorOtherBot() {
274+
World world = generateEmptyWorld((int) robot.getX(), (int) robot.getY(), 50);
275+
world.addRobot(robot);
276+
VM vm = robot.getVM();
277+
278+
float xOffset = 30.0f;
279+
280+
Robot dummy = new Robot(13);
281+
dummy.position(robot.getX() + xOffset, robot.getY());
282+
world.addRobot(dummy);
283+
284+
vm.push8(Robot.IO_SENSOR);
285+
vm.setIO(true);
286+
robot.handleIO(world);
287+
// Saw a robot...
288+
assertEquals(vm.pop8() & 0b111111, Robot.SENSOR_ROBOT);
289+
// ...closeby.
290+
assertEquals(vm.popFloat(), xOffset, Robot.COLLIDE_RANGE);
291+
}
292+
293+
@Test
216294
public void testBeamAngle() {
217295
World world = generateEmptyWorld((int) robot.getX(), (int) robot.getY(), 50);
218296
VM vm = robot.getVM();
@@ -234,6 +312,20 @@ public void testBeamAngle() {
234312
robot.handleIO(world);
235313
assertEquals((float)-Math.PI * 0.75f, robot.getBeamAngle(), 0.0001f);
236314
}
315+
316+
@Test
317+
public void testLazerDamage() {
318+
World world = generateEmptyWorld((int) robot.getX(), (int) robot.getY(), 50);
319+
Robot dummy = new Robot(13);
320+
dummy.position(robot.getX() + 30, robot.getY());
321+
world.addRobot(robot);
322+
world.addRobot(dummy);
323+
324+
int initialBattery = dummy.getBattery();
325+
robot.setLazer(1.0f);
326+
robot.tick(world);
327+
assert(dummy.getBattery() < initialBattery);
328+
}
237329

238330
private World generateEmptyWorld(int x, int y, int radius) {
239331
World world = new World();

0 commit comments

Comments
 (0)