Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor agent generation #255

Merged
merged 5 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/app/model/Coverage.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class Coverage

public Coverage(Map map)
{
System.out.println("Build coverage map ...");
width = map.getWidth();
height = map.getHeight();
vectors = new VectorSet();
Expand All @@ -30,6 +31,7 @@ public Coverage(Map map)
vectors.add(v);
}
}
System.out.print(" done");
}

public double percentSeen(VectorSet seen)
Expand Down
84 changes: 25 additions & 59 deletions src/main/java/app/model/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Stack;

Expand Down Expand Up @@ -72,58 +71,41 @@ public Map(Settings settings)
guardsSeen = new VectorSet();
intrudersSeen = new VectorSet();

// On creation add the right number of guards
/* On creation add the right number of guards */
for(int i = 0; i < settings.getNoOfGuards(); i++)
{
Vector srt = randPosition(guardSpawn);
if(srt != null)
{
Vector dir = randDirection();
Agent guard = new AcoAgentLimitedVision(srt, dir, 10, Team.GUARD);
guard.setMaxWalk(settings.getWalkSpeedGuard());
guard.setMaxSprint(settings.getSprintSpeedGuard());
agents.add(guard);
}
Vector dir = randDirection();
Agent guard = new AcoAgentLimitedVision(srt, dir, 10, Team.GUARD);
guard.setMaxWalk(settings.getWalkSpeedGuard());
guard.setMaxSprint(settings.getSprintSpeedGuard());
agents.add(guard);
}

// On creation add the right number of infiltrators
/* On creation add the right number of infiltrators */
for(int i = 0; i < settings.getNoOfIntruders(); i++)
{
Vector srt = randPosition(intruderSpawn);
if(srt != null)
{
Vector dir = randDirection();
Agent intruder = new WallFollowAgent(srt, dir, 10, Team.INTRUDER);
intruder.setTgtDirection(targetDirection(srt));
intruder.setMaxWalk(settings.getWalkSpeedIntruder());
intruder.setMaxSprint(settings.getSprintSpeedIntruder());
agents.add(intruder);
} else
{
i = settings.getNoOfIntruders();
}
Vector dir = randDirection();
Agent intruder = new WallFollowAgent(srt, dir, 10, Team.INTRUDER);
intruder.setMaxWalk(settings.getWalkSpeedIntruder());
intruder.setMaxSprint(settings.getSprintSpeedIntruder());
agents.add(intruder);
}

if(HUMAN_ACTIVE && intruderSpawn != null)
if (HUMAN_ACTIVE)
{
Vector humanStart = randPosition(intruderSpawn);
if(humanStart != null)
{
human = new Human(humanStart, new Vector(1, 0), 10, Team.INTRUDER);
//Assumes the human is a guard
human.setMaxWalk(settings.getWalkSpeedGuard());
human.setMaxSprint(settings.getSprintSpeedGuard());
agents.add(human);
}
human = new Human(humanStart, new Vector(1, 0), 10, Team.INTRUDER);
human.setMaxWalk(settings.getWalkSpeedGuard());
human.setMaxSprint(settings.getSprintSpeedGuard());
agents.add(human);
}

this.coverage = new Coverage(this);
System.out.println("done.");
System.out.print(" done");
}

/**
* Only for testing, delete later
*/
public Map(Agent agent, ArrayList<Furniture> obstacles)
{
agents = new ArrayList<>();
Expand Down Expand Up @@ -156,7 +138,6 @@ public void addSoundFurniture(SettingsObject obj)

public void addSoundSource(SettingsObject obj)
{
// TODO for now?
this.soundSources.add(SoundSourceFactory.make(SoundSourceType.SIREN, Vector.from(obj.getRect()), obj.getAmplitude()));
}

Expand Down Expand Up @@ -258,33 +239,18 @@ else if(r < 0.75)
private Vector randPosition(Rectangle2D r)
{
Vector v;
int tries = 0;
try
do
{
do
{
tries++;
double x = r.getMinX() + (Math.random() * (r.getMaxX() - r.getMinX()));
double y = r.getMinY() + (Math.random() * (r.getMaxY() - r.getMinY()));
v = new Vector(x, y);
if(tries > 500)
{
throw new RuntimeException("SpawnArea not big enough for number of agents");
}
} while(!clearSpot(v));

return v;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
double x = r.getMinX() + (Math.random() * (r.getMaxX() - r.getMinX()));
double y = r.getMinY() + (Math.random() * (r.getMaxY() - r.getMinY()));
v = new Vector(x, y);
} while(!clearSpot(v));
return v;
}

private boolean clearSpot(Vector v)
{
for(Agent agent : agents)
for(Agent agent: agents)
{
double dist = agent.getPosition().dist(v);
if(dist < 2 * agent.getRadius())
Expand Down
5 changes: 1 addition & 4 deletions src/test/java/testing/FileParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import app.controller.settings.Settings;
import app.model.Map;
import javafx.geometry.Rectangle2D;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -92,8 +93,6 @@ public class FileParserTest
assertEquals(new Vector(50, 90), s.getFurniture().get(6).getTeleportTo());
assertEquals(0.0, s.getFurniture().get(6).getTeleportRotation(), 0.001);
assertEquals("teleport", s.getFurniture().get(6).getType().label);

Map map = new Map(s);
}

@Test void testCreationOfSettingsNewMapWithComments()
Expand All @@ -119,7 +118,5 @@ public class FileParserTest
assertEquals(new Vector(50, 90), s.getFurniture().get(6).getTeleportTo());
assertEquals(0.0, s.getFurniture().get(6).getTeleportRotation(), 0.001);
assertEquals("teleport", s.getFurniture().get(6).getType().label);

Map map = new Map(s);
}
}