Skip to content

Commit

Permalink
Robots can now cross defenses. DEVELOPMENT IS NOW FROZEN. All further…
Browse files Browse the repository at this point in the history
… development will be done on the new 2017 game
  • Loading branch information
jakekinsella committed Oct 31, 2016
1 parent 7aa5a6e commit 0077f7a
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 5 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# FIRST Stronghold Simulator

As of now, development of this simulator is frozen due to the upcoming 2017 game. A simulator for the 2017 game will be created when the game is announced.

### What is it?
A simple simulator of the FRC 2016 game (Stronghold) that communicates to the server throught sockets. This simulates the basic Stronghold game but attempts to remove as many details as possible. For instance, instead of simulating pieces of the robot, the robot just tells the server how long it takes to shoot and how accurate it is.
Expand All @@ -12,4 +14,7 @@ As you can see, many pieces that are often considered an integral part of FRC ha
- Randomize the position of the ball after a missed highgoal shot
- I don't want to deal with the physics part so just make sure the robot can't learn from the ball's end position
- Implement climbing the tower
- Implement defense crossing

### Bugs
- Cannot move in amounts smaller than about 100 px/s
- Can change action during defense cross and get stuck in the defense
6 changes: 6 additions & 0 deletions docs/test_socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@
i = 0
while true

puts s.gets.chomp

if i == 0
s.puts "{\"command\": \"MOVE\",\"args\": [0.5]}"
end

if i == 5
s.puts "{\"command\": \"DEFENSE\",\"args\": [4000]}"
end

i += 1

sleep(1)
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/gmail/jakekinsella/field/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ public ArrayList<Defense> detectAllDefensesInRect(Shape rectangle) {
return detectedDefenses;
}

public Defense robotInDefenseDetector(Shape rectangle) {
ArrayList<Defense> defenses = new ArrayList<>(this.redAlliance.getDefenses());
defenses.addAll(this.blueAlliance.getDefenses());

for (Defense defense : defenses) {
if (defense.getDetectionBox().intersects(rectangle.getBounds().getCenterX(), rectangle.getBounds().getCenterY(), 1, 11)) {
return defense;
}
}

return null;
}

public boolean touchingWall(Shape rectangle) {
return rectangle.intersects(this.leftWall) || rectangle.intersects(this.rightWall) || rectangle.intersects(this.topWall) || rectangle.intersects(this.bottomWall);
}
Expand Down
23 changes: 22 additions & 1 deletion src/main/java/com/gmail/jakekinsella/field/defense/Defense.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,26 @@ public abstract class Defense implements Paintable {
protected RobotAllianceColor allianceColor;

private Rectangle2D.Double rectangle;
private Rectangle2D.Double detectionBox;

public Defense(int defensePosition, RobotAllianceColor allianceColor) {
this.defensePosition = defensePosition;
this.allianceColor = allianceColor;

int[] position = this.getPosition(allianceColor, defensePosition);
int[] position = getPosition(allianceColor, defensePosition);
this.x = position[0];
this.y = position[1];

this.rectangle = new Rectangle2D.Double(this.x - 8, this.y - 6, 69, 62);

int xDetection = this.x;
if (this.allianceColor.equals(RobotAllianceColor.BLUE)) {
xDetection += 55;
} else {
xDetection -= 25;
}

this.detectionBox = new Rectangle2D.Double(xDetection, this.y + 20, 30, 10);
}

public String getDefenseName() {
Expand All @@ -40,11 +50,22 @@ public Rectangle2D.Double getRectangle() {
return this.rectangle;
}

public Rectangle2D.Double getDetectionBox() {
return this.detectionBox;
}

public RobotAllianceColor getAllianceColor() {
return allianceColor;
}

@Override
public void paint(Graphics graphics, Graphics2D graphics2D) {
graphics2D.setColor(Color.BLUE);
graphics2D.draw(this.getRectangle());

graphics2D.setColor(Color.YELLOW);
graphics2D.draw(this.getDetectionBox());

graphics.setFont(new Font("Arial", Font.PLAIN, 6));
graphics.setColor(Color.BLACK);
graphics.drawString(this.defenseName, this.x, this.y);
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/gmail/jakekinsella/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ public void movementTick(long delta) {
this.setY((int) (this.getY() + deltaY));

if (RobotServer.getField().detectAllDefensesInRect(this.getRectangle()).size() > 0) {
this.setVelocity(0);
if (!this.currentAction.toString().equals("DEFENSE")) {
this.setVelocity(0);
}

if (this.currentAction.toString().equals("TURN")) {
this.currentAction = new NoneAction();
Expand Down Expand Up @@ -296,6 +298,10 @@ public void processRobotInput() {
logger.info(this.getRobotName() + " has started to shoot a lowgoal");
this.currentAction = new LowgoalAction(commandInfo, this, this.lowgoalTime, this.lowgoalChance, this.lowgoalSide, this.lowgoalRange);
break;
case DEFENSE:
logger.info(this.getRobotName() + " has started to cross a defense");
this.currentAction = new DefenseAction(commandInfo, this);
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.gmail.jakekinsella.robotactions;

import com.gmail.jakekinsella.field.defense.Defense;
import com.gmail.jakekinsella.robot.Robot;
import com.gmail.jakekinsella.robot.RobotAllianceColor;
import com.gmail.jakekinsella.robot.RobotServer;
import com.gmail.jakekinsella.socketcommands.Command;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
* Created by jakekinsella on 10/31/16.
*/
public class DefenseAction extends TimeAction {

private int direction = 1;

private static final Logger logger = LogManager.getLogger();

public DefenseAction(Command command, Robot robot) {
super(command, robot, ((Long) command.getArg(0)).intValue());

this.success = true;
}

@Override
public void actionStart() {

if (this.robot.getVelocity() != 0) {
success = false;
logger.info(this.robot.getRobotName() + " is at a bad velocity to cross a defense!");
}

Defense defense = RobotServer.getField().robotInDefenseDetector(this.robot.getRectangle());
if (defense != null) {
if (defense.getAllianceColor().equals(RobotAllianceColor.BLUE)) {
this.direction = -1;
}

if (this.robot.getAngle() != this.direction * 90) {
success = false;
logger.info(this.robot.getRobotName() + " is at a bad angle to cross a defense!");
}
} else {
success = false;
logger.info(this.robot.getRobotName() + " is not in position to cross a defense!");
}

if (!success) {
this.robot.actionFinish();
this.robot.sendActionResponse();
} else {
this.robot.setVelocity((-1 * this.direction * 50.0) / (((Long) command.getArg(0)).intValue() / 1000.0));
}
}

@Override
public void actionDone() {
logger.info(this.robot.getRobotName() + " has crossed a defense!");

this.robot.setVelocity(0);
this.robot.getRobotAlliance().getScore().scoreDefenseCross();
this.robot.sendActionResponse();
this.robot.actionFinish();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class TimeAction extends Action {

private long remainingTime;
private long lastTick;
private int i = 0;

public TimeAction(Command command, Robot robot, int time) {
super(command, robot);
Expand All @@ -20,7 +21,9 @@ public TimeAction(Command command, Robot robot, int time) {

@Override
public void tick() {
actionStart();
if (i == 0) {
actionStart();
}

long delta = System.currentTimeMillis() - this.lastTick;
this.remainingTime -= delta;
Expand All @@ -32,6 +35,8 @@ public void tick() {
}

this.lastTick = System.currentTimeMillis();

i++;
}

public void actionDone() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public enum ClientCommand {
TURN,
SHOOT,
PICKUP,
LOWGOAL
LOWGOAL,
DEFENSE
}

0 comments on commit 0077f7a

Please sign in to comment.