// Store information on whether each red unit is carrying a civilian or not void updateRedUnitDatabase() { // Clear redUnitDatabase.Clear(); // Get all red units redUnits = new List <GameObject>(); redUnits.AddRange(GameObject.FindGameObjectsWithTag("RedTruck")); redUnits.AddRange(GameObject.FindGameObjectsWithTag("RedDismount")); // Loop through all actor beliefs and update the database foreach (Belief_Actor b in actorBeliefs) { // We care only about red units Belief_Actor ba = (Belief_Actor)b; if (ba.getAffiliation().Equals((int)Affiliation.RED)) { // Save the info if the belief is new enough // Note: Watch out for the difference of two unsigned numbers! if (thisSoaActor.getCurrentTime_ms() <= ba.getBeliefTime() || (float)(thisSoaActor.getCurrentTime_ms() - ba.getBeliefTime()) <= beliefTimeout_ms) { RedUnitInfo redUnitInfo = new RedUnitInfo(ba, redUnits, redBases, protectedSites); if (redUnitInfo.distToClosestRedBase > redBaseKeepoutDist * SimControl.KmToUnity) { redUnitDatabase.Add(ba.getId(), new RedUnitInfo(ba, redUnits, redBases, protectedSites)); } } } } }
public virtual void UpdatePose() { simX_km = transform.position.x / SimControl.KmToUnity; simZ_km = transform.position.z / SimControl.KmToUnity; if (nma != null) { velocityX = (nma.velocity.x / SimControl.KmToUnity) / 60f * 1000f; velocityXValid = true; velocityY = (nma.velocity.y / SimControl.KmToUnity) / 60f * 1000f; velocityYValid = true; velocityZ = (nma.velocity.z / SimControl.KmToUnity) / 60f * 1000f; velocityZValid = true; //Debug.Log("VELOCITY " + unique_id + " " + (nma.velocity.magnitude / 60f * 1000f)); } Belief_Actor newActorData = new Belief_Actor( unique_id, (int)affiliation, type, isAlive, numStorageSlots, numCasualtiesStored, numSuppliesStored, numCiviliansStored, isWeaponized, hasJammer, fuelRemaining_s, simX_km, simAltitude_km, simZ_km, velocityXValid, velocityX, velocityYValid, velocityY, velocityZValid, velocityZ); addMyBeliefData(newActorData); }
// Called when the actor has been killed public virtual void Kill(string killerName) { if (isAlive) { // Set that it is no longer alive isAlive = false; // Log event if (gameObject.CompareTag("HeavyUAV")) { simControlScript.soaEventLogger.LogHeavyUAVLost(gameObject.name, killerName); } else if (gameObject.CompareTag("SmallUAV")) { simControlScript.soaEventLogger.LogSmallUAVLost(gameObject.name, killerName); } // Convert position from Unity to km for Belief_Actor Belief_Actor newActorData = new Belief_Actor( unique_id, (int)affiliation, type, isAlive, numStorageSlots, numCasualtiesStored, numSuppliesStored, numCiviliansStored, isWeaponized, hasJammer, fuelRemaining_s, transform.position.x / SimControl.KmToUnity, simAltitude_km, transform.position.z / SimControl.KmToUnity); //addBeliefToUnmergedBeliefDictionary(newActorData); addMyBeliefData(newActorData); //addBeliefToBeliefDictionary(newActorData); //addBelief(newActorData, remoteBeliefs); // Don't move anymore simulateMotion = false; if (navAgent != null) { navAgent.ResetPath(); navAgent.enabled = false; } if (motionScript != null) { motionScript.enabled = false; } // TODO: Make sure dead unit does not act as relay for comms // Change appearance to look destroyed Vector3 destroyedPos = transform.position; destroyedPos.y = 0.85f; transform.position = destroyedPos; } }
public RedUnitInfo(Belief_Actor ba, List <GameObject> redUnits, List <GameObject> redBases, List <GameObject> protectedSites) { // Get ID id = ba.getUnique_id(); // Extract 6 state (in Unity space) pos = new Vector3( ba.getPos_x() * SimControl.KmToUnity, ba.getPos_y() * SimControl.KmToUnity, ba.getPos_z() * SimControl.KmToUnity); vel = new Vector3( ba.getVelocity_x() * SimControl.KmToUnity, ba.getVelocity_y() * SimControl.KmToUnity, ba.getVelocity_z() * SimControl.KmToUnity); velIsValid = ba.getVelocity_x_valid() && ba.getVelocity_y_valid() && ba.getVelocity_z_valid(); // Not a pursue candidate by default isCatchable = false; // Compute distances computeProximityToProtectedSites(protectedSites); computeProximityToRedBases(redBases); // Look up Unity specific properties foreach (GameObject g in redUnits) { // Find the corresponding game object if (g.GetComponent <SoaActor>().unique_id == id) { // Save hasCivilian switch (g.tag) { case "RedDismount": case "RedTruck": hasCivilian = g.GetComponent <SoaActor>().numCiviliansStored > 0; break; default: // Unrecognized Debug.LogError("RedUnitInfo(): Unrecognized g.tag " + g.tag); break; } // Save max speed (in Unity coordinates) maxSpeed = g.GetComponent <NavMeshAgent>().speed; } } }
/** * Update the position of the actor vectors. This function is called when in simulation mode * If Sim is in charge of waypoints, get the current target from the motion script and add to * belief map. Otherwise get the waypointBelief from the dictionary and set as navAgent destination. * * TODO create functions for when in client mode and only reading position data */ public virtual void updateActor() { if (!isAlive) { return; } Belief myActor = null; displayTruePosition = true; if (myActor != null && myActor.getId() != unique_id) { Debug.LogError("Update Actor " + unique_id + " has invalid id " + myActor.getId()); return; } if (simulateMotion) { displayActor = true; // Setup waypoint belief if (useExternalWaypoint) { Belief_Waypoint newWaypoint = beliefRepo.Find <Belief_Waypoint>(Belief.BeliefType.WAYPOINT, unique_id); Belief_WaypointPath newPath = beliefRepo.Find <Belief_WaypointPath>(Belief.BeliefType.WAYPOINT_PATH, unique_id); motionScript.On = false; if (wpMotionScript != null) { if (newPath != null && newWaypoint != null) { //If we have a path and a waypoint choose the newest one if (newPath.getBeliefTime() < newWaypoint.getBeliefTime()) { newPath = null; } else { newWaypoint = null; } } if (newPath != null) { wpMotionScript.SetWaypointBelief(newPath); wpMotionScript.On = true; } else if (newWaypoint != null) { wpMotionScript.SetWaypointBelief(newWaypoint); wpMotionScript.On = true; } //SetDesiredAltitude(wpMotionScript.desiredAltitude_km); } else if (newWaypoint != null) { // Received a waypoint in km, transform to Unity coordinates and then use it to set the nav agent's destination if (navAgent != null) { navAgent.SetDestination(SimControl.ConstrainUnityDestinationToBoard( new Vector3( newWaypoint.getPos_x() * SimControl.KmToUnity, transform.position.y, // Nav agent ignores the y coordinate (altitude) newWaypoint.getPos_z() * SimControl.KmToUnity ) )); } // Set the desired altitude separately [km] SetDesiredAltitude(newWaypoint.getPos_y()); } else if (navAgent != null) { // Stay put navAgent.SetDestination(SimControl.ConstrainUnityDestinationToBoard( new Vector3(transform.position.x, transform.position.y, transform.position.z))); } } else if (motionScript != null) { // Convert position coordinates from unity to km before making new belief waypoint Belief_Waypoint newWaypoint = new Belief_Waypoint((ulong)(System.DateTime.UtcNow - epoch).Milliseconds, unique_id, motionScript.targetPosition.x / SimControl.KmToUnity, desiredAltitude_km, motionScript.targetPosition.z / SimControl.KmToUnity); addMyBeliefData(newWaypoint); } else if (wpMotionScript != null) { // Special case for blue balloon // Convert position coordinates from unity to km before making new belief waypoint /* * Belief_Waypoint newWaypoint = new Belief_Waypoint((ulong)(System.DateTime.UtcNow - epoch).Milliseconds, unique_id, * wpMotionScript.targetPosition.x / SimControl.KmToUnity, * desiredAltitude_km, * wpMotionScript.targetPosition.z / SimControl.KmToUnity); * addMyBeliefData(newWaypoint); */ Belief_WaypointPath waypointPath = beliefRepo.Find <Belief_WaypointPath>(Belief.BeliefType.WAYPOINT_PATH, unique_id); if (waypointPath != null) { wpMotionScript.SetWaypointBelief(waypointPath); } } else { // Special case for blue balloon // Convert position coordinates from unity to km before making new belief waypoint Belief_Waypoint newWaypoint = new Belief_Waypoint((ulong)(System.DateTime.UtcNow - epoch).Milliseconds, unique_id, pcMotionScript.targetPosition.x / SimControl.KmToUnity, desiredAltitude_km, pcMotionScript.targetPosition.z / SimControl.KmToUnity); addMyBeliefData(newWaypoint); } // Clear and update classifications foreach (SoaClassifier c in Classifiers) { c.UpdateClassifications(Detections); } // Update each weapon foreach (SoaWeapon w in Weapons) { w.UpdateWeapon(Detections); } // Go through each detected object's Soa Actor, get unique ID, affiliation, and pos. Broadcast belief out to everyone // Send detections to Sim Controller to be logged foreach (GameObject gameObject in Detections) { SoaActor soaActor = gameObject.GetComponent <SoaActor>(); // Actor's position must be converted from Unity to km when generating belief Belief_Actor detectedActor; if (classificationDictionary.ContainsKey(soaActor.unique_id) && classificationDictionary[soaActor.unique_id]) { // Me or one of my peers has classified this actor before, provide actual affiliation, isWeaponized info, and storage detectedActor = new Belief_Actor( soaActor.unique_id, (int)soaActor.affiliation, soaActor.type, soaActor.isAlive, soaActor.numStorageSlots, soaActor.numCasualtiesStored, soaActor.numSuppliesStored, soaActor.numCiviliansStored, soaActor.isWeaponized, soaActor.hasJammer, soaActor.fuelRemaining_s, gameObject.transform.position.x / SimControl.KmToUnity, soaActor.simAltitude_km, gameObject.transform.position.z / SimControl.KmToUnity); } else { // No one that I'm aware of has classified this actor before, hide affiliation, weapon/jamming, and storage detectedActor = new Belief_Actor( soaActor.unique_id, (int)Affiliation.UNCLASSIFIED, soaActor.type, soaActor.isAlive, 0, 0, 0, 0, false, false, soaActor.fuelRemaining_s, gameObject.transform.position.x / SimControl.KmToUnity, soaActor.simAltitude_km, gameObject.transform.position.z / SimControl.KmToUnity); } addMyBeliefData(detectedActor); simControlScript.logDetectedActor(unique_id, detectedActor); } Detections.Clear(); //Evaluate if grid cell changed and log if (prevHexGridCell != null) { currentCell = hexGrid.Map[transform.position]; if (prevHexGridCell != currentCell) { prevHexGridCell = currentCell; simControlScript.logGridCellMove(unique_id, currentCell.X, currentCell.Y); //Debug.LogWarning("Actor " + unique_id + " Moved to cell " + prevHexGridCell.X + " " + prevHexGridCell.Y); } } else { currentCell = hexGrid.Map[transform.position]; prevHexGridCell = currentCell; } List <Belief_Actor> localKillDetections = new List <Belief_Actor>(); lock (killDetections) { localKillDetections.AddRange(killDetections); killDetections.Clear(); } foreach (Belief_Actor belief_actor in localKillDetections) { addMyBeliefData(new Belief_Actor( belief_actor.getId(), (int)belief_actor.getAffiliation(), belief_actor.getType(), false, belief_actor.getNumStorageSlots(), belief_actor.getNumCasualtiesStored(), belief_actor.getNumSuppliesStored(), belief_actor.getNumCiviliansStored(), belief_actor.getIsWeaponized(), belief_actor.getHasJammer(), belief_actor.getFuelRemaining(), belief_actor.getPos_x(), belief_actor.getPos_y(), belief_actor.getPos_z())); } // ??? useGhostModel = false; } }