diff --git a/assignments/src/MV3500Cohort2024JulySeptember/homework3/Romero/RomeroSimulationProgram.java b/assignments/src/MV3500Cohort2024JulySeptember/homework3/Romero/RomeroSimulationProgram.java index 8c3664dfb408fe2a5bfe73f548320cda92ed25cd..70123212971acaa76226f943f5c5516255957ed5 100644 --- a/assignments/src/MV3500Cohort2024JulySeptember/homework3/Romero/RomeroSimulationProgram.java +++ b/assignments/src/MV3500Cohort2024JulySeptember/homework3/Romero/RomeroSimulationProgram.java @@ -5,13 +5,23 @@ */ package MV3500Cohort2024JulySeptember.homework3.Romero; -import edu.nps.moves.dis7.entities.swe.platform.surface._001Poseidon; -import edu.nps.moves.dis7.entities.swe.platform.surface._002Triton; +//import edu.nps.moves.dis7.entities.swe.platform.surface._001Poseidon; +//import edu.nps.moves.dis7.entities.swe.platform.surface._002Triton; +//https://savage.nps.edu/opendis7-java/javadoc/edu/nps/moves/dis7/entities/usa/platform/land/ChevyS10ShooterwAK74.html +import edu.nps.moves.dis7.entities.usa.platform.land.M41lighttank; + +//https://savage.nps.edu/opendis7-java/javadoc/edu/nps/moves/dis7/entities/usa/platform/land/ChenowthLightStrikeVehicleLSV.html +import edu.nps.moves.dis7.entities.usa.platform.land.ChenowthLightStrikeVehicleLSV; + +//https://savage.nps.edu/open-dis7-java/javadoc/edu/nps/moves/dis7/entities/chn/munition/tactical/Ballistic.html +//import edu.nps.moves.dis7.entities.usa.*; +import edu.nps.moves.dis7.enumerations.VariableRecordType; import edu.nps.moves.dis7.enumerations.*; import edu.nps.moves.dis7.pdus.*; import edu.nps.moves.dis7.utilities.DisChannel; import edu.nps.moves.dis7.utilities.PduFactory; import java.time.LocalDateTime; +import java.util.Random; import java.util.logging.Level; import java.util.logging.Logger; @@ -42,7 +52,7 @@ public class RomeroSimulationProgram /** current simulation time in seconds */ double simulationTimeSeconds = simulationTimeInitial; /** Maximum number of simulation loops */ - int MAX_LOOP_COUNT = 4; + int MAX_LOOP_COUNT = 10; String narrativeMessage1 = new String(); String narrativeMessage2 = new String(); @@ -52,19 +62,31 @@ public class RomeroSimulationProgram protected EntityID entityID_1 = new EntityID(); /** EntityID settings for entity 2 */ protected EntityID entityID_2 = new EntityID(); + protected EntityID entityID_3 = new EntityID(); /** ESPDU for entity 1 */ protected EntityStatePdu entityStatePdu_1; /** ESPDU for entity 2 */ protected EntityStatePdu entityStatePdu_2; + /** ESPDU for entity 3 */ + protected EntityStatePdu entityStatePdu_3; /** FirePdu for entity 1 first weapon (if any) */ - protected FirePdu firePdu_1a; + protected FirePdu firePdu_1a = new FirePdu(); /** FirePdu for entity 1 second weapon (if any) */ protected FirePdu firePdu_1b; /** MunitionDescriptor for these weapons */ - protected MunitionDescriptor munitionDescriptor1; + protected MunitionDescriptor munition; // hey programmer, what other state do you want? this is a good place to declare it... + protected EntityType entityType_1; + protected EntityType entityType_2; + protected EntityType entityType_3; + protected EntityType munitionType = new EntityType(); + + Vector3Double location_1 = new Vector3Double(); + Vector3Double location_2 = new Vector3Double(); + Vector3Double location_3 = new Vector3Double(); + /** * Constructor to create an instance of this class. * Design goal: additional built-in initialization conveniences can go here @@ -139,7 +161,7 @@ public class RomeroSimulationProgram // TODO confirm whether recorder is explicitly started by programmer (or not) -// disChannel.sendCommentPdu(VariableRecordType.OTHER, "DisThreadedNetworkInterface.initializeDisChannel() complete"); // hello channel, debug + //disChannel.sendCommentPdu(VariableRecordType.OTHER, "DisThreadedNetworkInterface.initializeDisChannel() complete"); // hello channel, debug } /** Get ready, get set... initialize simulation entities. Who's who in the zoo? @@ -150,39 +172,110 @@ public class RomeroSimulationProgram pduFactory = disChannel.getPduFactory(); entityStatePdu_1 = pduFactory.makeEntityStatePdu(); entityStatePdu_2 = pduFactory.makeEntityStatePdu(); + entityStatePdu_3 = pduFactory.makeEntityStatePdu(); firePdu_1a = pduFactory.makeFirePdu(); - firePdu_1b = pduFactory.makeFirePdu(); - munitionDescriptor1 = new MunitionDescriptor(); - + //firePdu_1b = pduFactory.makeFirePdu(); + munition = new MunitionDescriptor(); + // Your model setup: define participants. who's who in this zoo? // Assuming you keep track of entity objects... here is some support for for Entity 1. - // PDU objects are already declared and instances created, so now set their values. - // who is who in our big zoo, sufficient for global participation if we need it - entityID_1.setSiteID(1).setApplicationID(2).setEntityID(3); // made-up example ID; - disChannel.addEntity(entityID_1); - - entityID_2.setSiteID(1).setApplicationID(2).setEntityID(4); // made-up example ID; - disChannel.addEntity(entityID_2); - // TODO someday, use enumerations for sites as part of a SimulationManager object; e.g. is there a unique site triplet for MOVES Institute? + try { + Thread.sleep(2000); + System.out.println("\nWho's who in this zoo?\n"); + Thread.sleep(2000); + + // PDU objects are already declared and instances created, so now set their values. + // who is who in our big zoo, sufficient for global participation if we need it + entityID_1.setSiteID(1).setApplicationID(2).setEntityID(3); // made-up example ID; + disChannel.addEntity(entityID_1); - entityStatePdu_1.setEntityID(entityID_1); - entityStatePdu_1.setForceId(ForceID.FRIENDLY); - entityStatePdu_1.setEntityType(new _001Poseidon()); // note import statement above -// entityStatePdu_1.setMarking("Entity #1"); - entityStatePdu_1.setEntityType(new edu.nps.moves.dis7.entities.usa.platform.air.MV22B()); // note import statement at top - entityStatePdu_1.setMarking("Entity #53"); - entityStatePdu_1.getMarkingString(); // use Netbeans Debug breakpoint here to check left justified... + entityID_2.setSiteID(1).setApplicationID(2).setEntityID(4); // made-up example ID; + disChannel.addEntity(entityID_2); + // TODO someday, use enumerations for sites as part of a SimulationManager object; e.g. is there a unique site triplet for MOVES Institute? - entityStatePdu_2.setEntityID(entityID_2); - entityStatePdu_2.setForceId(ForceID.OPPOSING); - entityStatePdu_2.setEntityType(new _002Triton()); // note import statement above - entityStatePdu_2.setMarking("Entity #2"); + entityID_3.setSiteID(1).setApplicationID(2).setEntityID(5); // made-up example ID; + disChannel.addEntity(entityID_3); - // TODO how should we customize this munition? what are key parameters for your simulation? - // more is needed here by scenario authors... - munitionDescriptor1.setQuantity(1); - firePdu_1a.setDescriptor(munitionDescriptor1).setRange(1000.0f); + + //********************************************************************************* + + entityStatePdu_1.setEntityID(entityID_1); + entityStatePdu_1.setForceId(ForceID.OPPOSING); + entityStatePdu_1.setEntityType(new M41lighttank()); // note import statement above + // entityStatePdu_1.setMarking("Entity #1"); + //entityStatePdu_1.setEntityType(new edu.nps.moves.dis7.entities.usa.platform.air.MV22B()); // note import statement at top + entityStatePdu_1.setMarking("Entity #001"); + entityStatePdu_1.getMarkingString(); // use Netbeans Debug breakpoint here to check left justified... + location_1.setX(500.0); + location_1.setY(1000.0); + location_1.setZ(0.0); + entityStatePdu_1.setEntityLocation(location_1); + firePdu_1a.setFiringEntityID(entityID_1); + //firePdu_1a.setLocationInWorldCoordinates(location_1); + + System.out.println("OPPOSING entity: " + entityStatePdu_1.getEntityType() + "\n"); + System.out.println("ID: " + entityStatePdu_1.getEntityID() + "\n"); + System.out.println("Location: " + entityStatePdu_1.getEntityLocation() + "\n"); + + Thread.sleep(1000); + +// munitionType.setEntityKind(EntityKind.MUNITION); +// munitionType.setDomain((short) ) + + munition.setQuantity(30); + munition.setMunitionType(munitionType); + System.out.println("Munition: " + munition.getMunitionType() + "\n"); + firePdu_1a.setDescriptor(munition).setRange(600.0f); + + Vector3Float munitionVelocity = new Vector3Float(); + munitionVelocity.setX(200.0f); + munitionVelocity.setY(0.0f); + munitionVelocity.setZ(0.0f); + firePdu_1a.setVelocity(munitionVelocity); + Thread.sleep(4000); + + //**************************************************** + + entityStatePdu_2.setEntityID(entityID_2); + entityStatePdu_2.setForceId(ForceID.FRIENDLY); + entityStatePdu_2.setEntityType(new ChenowthLightStrikeVehicleLSV()); // note import statement above + entityStatePdu_2.setMarking("Entity #2"); + location_2.setX(1000.0); + location_2.setY(750.0); + location_2.setZ(0.0); + entityStatePdu_2.setEntityLocation(location_2); + firePdu_1a.setTargetEntityID(entityID_2); + + System.out.println("\n********\nFRIENDLY unit: " + entityStatePdu_2.getEntityType() + "\n"); + System.out.println("ID: " + entityStatePdu_2.getEntityID() + "\n"); + System.out.println("Initial Location: " + entityStatePdu_2.getEntityLocation() + "\n"); + Thread.sleep(4000); + + //***************************************************** + + entityStatePdu_3.setEntityID(entityID_3); + entityStatePdu_3.setForceId(ForceID.FRIENDLY); + entityStatePdu_3.setEntityType(new ChenowthLightStrikeVehicleLSV()); // note import statement above + entityStatePdu_3.setMarking("Entity #3"); + location_3.setX(0.0); + location_3.setY(250.0); + location_3.setZ(0.0); + entityStatePdu_3.setEntityLocation(location_3); + firePdu_1a.setTargetEntityID(entityID_3); + + System.out.println("FRIENDLY unit: " + entityStatePdu_3.getEntityType() + "\n"); + System.out.println("ID: " + entityStatePdu_3.getEntityID() + "\n"); + System.out.println("Initial Location: " + entityStatePdu_3.getEntityLocation() + "\n********\n"); + Thread.sleep(4000); + + // TODO how should we customize this munition? what are key parameters for your simulation? + // more is needed here by scenario authors... + + + } catch (InterruptedException e) { + e.printStackTrace(); + } } /** @@ -207,6 +300,8 @@ public class RomeroSimulationProgram int simulationLoopCount = 0; // variable, initialized at 0 boolean simulationComplete = false; // sentinel variable as termination condition, are we done yet? + int countImpactE2 = 0; + int countImpactE3 = 0; // TODO reset Clock Time for today's date and timestamp to zero, providing consistent outputs for each simulation run String timeMessage = "Simulation time " + simulationTimeSeconds + " at LocalDateTime " + LocalDateTime.now(); disChannel.sendCommentPdu(simulationTimeSeconds, DisChannel.COMMENTPDU_TIME, timeMessage); @@ -222,7 +317,18 @@ public class RomeroSimulationProgram // ============================================================================================= // * your own simulation code starts here! ***************************************************** // ============================================================================================= + Random random = new Random(); + double randomX = 0 + (950 - 0) * random.nextDouble(); + double randomY = 0 + (750 - 0) * random.nextDouble(); + double radius = 200.0; + + Vector3Double targetLocation = new Vector3Double(); + targetLocation.setX(randomX); + targetLocation.setY(randomY); + targetLocation.setZ(0.0); + firePdu_1a.setLocationInWorldCoordinates(targetLocation); + // are there any other variables to modify at the beginning of your loop? // are your reading any DIS PDUs from the network? check for them here @@ -230,14 +336,38 @@ public class RomeroSimulationProgram // compute a track, update an ESPDU, whatever it is that your model is doing... // Where is my entity? Insert changes in position; this sample only changes X position. - entityStatePdu_1.getEntityLocation().setX(entityStatePdu_1.getEntityLocation().getX() + 1.0); // 1m per timestep + entityStatePdu_2.getEntityLocation().setX(entityStatePdu_2.getEntityLocation().getX() - 100.0); // 1m per timestep + entityStatePdu_2.getEntityLocation().setY(entityStatePdu_2.getEntityLocation().getY() + 10.0); // 1m per timestep + + entityStatePdu_3.getEntityLocation().setX(entityStatePdu_3.getEntityLocation().getX() + 100.0); // 1m per timestep + entityStatePdu_3.getEntityLocation().setY(entityStatePdu_3.getEntityLocation().getY() + 10.0); // 1m per timestep + + double distanceToImpact2 = calculateDistance(targetLocation, entityStatePdu_2.getEntityLocation()); + double distanceToImpact3 = calculateDistance(targetLocation, entityStatePdu_3.getEntityLocation()); + + System.out.println ("... Unit position is: " + entityStatePdu_2.getEntityLocation().getX() + ", " + entityStatePdu_2.getEntityLocation().getY()); + System.out.println ("... Unit position is: " + entityStatePdu_3.getEntityLocation().getX() + ", " + entityStatePdu_3.getEntityLocation().getY()); + + System.out.println("Firing at: " + targetLocation); + + if (distanceToImpact2 <= radius) { + System.out.println("-----> Entity #2 HITTED!!!"); + countImpactE2++; + } else if (distanceToImpact3 <= radius) { + System.out.println("-----> Entity #3 HITTED!!!"); + countImpactE3++; + } else { + System.out.println("No impacts detected!!"); + } + + Thread.sleep(1500); // decide whether to fire, and then update the firePdu. Hmmm, you might want a target to shoot at! // etc. etc. your code goes here for your simulation of interest // something happens between my simulation entities, la de da de da... - System.out.println ("... My simulation just did something, no really..."); + //System.out.println ("... My simulation just did something, no really..."); System.out.flush(); // make sure this arrives to user even if other threads somehow become deadlocked @@ -288,9 +418,16 @@ public class RomeroSimulationProgram } // end of simulation loop, continue until done // ===================================================================================================// ===================================================================================================// ===================================================================================================// =================================================================================================== - + narrativeMessage2 = "runSimulation() completed successfully"; // all done, so tell everyone else on the channel // TODO better javadoc needs to be autogenerated for VariableRecordType enumerations + + System.out.println("=========================="); + System.out.println("Last Location for Entity #2: " + entityStatePdu_2.getEntityLocation()); + System.out.println("Last Location for Entity #3: " + entityStatePdu_3.getEntityLocation()); + System.out.println("Total impacts to Entity #2: " + countImpactE2); + System.out.println("Total impacts to Entity #3: " + countImpactE3); + disChannel.sendCommentPdu(DisChannel.COMMENTPDU_NARRATIVE, narrativeMessage1, narrativeMessage2, narrativeMessage3); System.out.println ("... [final=completion CommentPdu successfully sent for simulation]"); @@ -350,6 +487,13 @@ public class RomeroSimulationProgram System.exit(-1); } } + + public double calculateDistance (Vector3Double loc1, Vector3Double loc2) { + double dx = loc2.getX() - loc1.getX(); + double dy = loc2.getY() - loc1.getY(); + double dz = loc2.getZ() - loc1.getZ(); + return Math.sqrt(dx * dx + dy * dy + dz * dz); + } /** * Get simple descriptor (such as parent class name) for this network interface, used in trace statements diff --git a/assignments/src/MV3500Cohort2024JulySeptember/homework3/Romero/Shot.png b/assignments/src/MV3500Cohort2024JulySeptember/homework3/Romero/Shot.png new file mode 100644 index 0000000000000000000000000000000000000000..7384dd156e786c6ccebbb93bc313e0c0cfd3ffed Binary files /dev/null and b/assignments/src/MV3500Cohort2024JulySeptember/homework3/Romero/Shot.png differ diff --git a/assignments/src/MV3500Cohort2024JulySeptember/homework3/Romero/Shot1.png b/assignments/src/MV3500Cohort2024JulySeptember/homework3/Romero/Shot1.png new file mode 100644 index 0000000000000000000000000000000000000000..891f32a96140cee2603cf77ed6823d5d2038d28f Binary files /dev/null and b/assignments/src/MV3500Cohort2024JulySeptember/homework3/Romero/Shot1.png differ