public void SetWaypointBelief(soa.Belief_Waypoint belief) { lock (waypoints) { if (belief != lastBelief && belief != null) { this.waypoints.Clear(); Vector3 location = new Vector3(belief.getPos_x() * SimControl.KmToUnity, belief.getPos_y() * SimControl.KmToUnity, belief.getPos_z() * SimControl.KmToUnity); waypoints.Add(new Waypoint(location, belief.getSpeed())); waypointIndex = 0; lastBelief = belief; UpdateDisplay(); } } }
// Serialize a Belief object into a byte array using Google Protobuf + 1 header byte public override byte[] serializeBelief(Belief belief) { // Variables to hold serialized message byte header; byte[] body, message; // Initialize based on type of belief switch (belief.getBeliefType()) { case Belief.BeliefType.ACTOR: { // Actor Gpb_Actor.Builder proto = Gpb_Actor.CreateBuilder(); Belief_Actor b = (Belief_Actor)belief; proto.SetUniqueId(b.getUnique_id()); proto.SetAffiliation(b.getAffiliation()); proto.SetType(b.getType()); proto.SetIsAlive(b.getIsAlive()); proto.SetNumStorageSlots(b.getNumStorageSlots()); proto.SetNumCasualtiesStored(b.getNumCasualtiesStored()); proto.SetNumSuppliesStored(b.getNumSuppliesStored()); proto.SetNumCiviliansStored(b.getNumCiviliansStored()); proto.SetIsWeaponized(b.getIsWeaponized()); proto.SetHasJammer(b.getHasJammer()); proto.SetFuelRemaining(b.getFuelRemaining()); proto.SetPosX(b.getPos_x()); proto.SetPosY(b.getPos_y()); proto.SetPosZ(b.getPos_z()); // Optional fields if (b.getVelocity_x_valid()) { proto.SetVelocityX(b.getVelocity_x()); } if (b.getVelocity_y_valid()) { proto.SetVelocityY(b.getVelocity_y()); } if (b.getVelocity_z_valid()) { proto.SetVelocityZ(b.getVelocity_z()); } // Add on belief time proto.SetBeliefTime(b.getBeliefTime()); // Form header + serialized message header = (byte)MessageType.ACTOR; body = proto.Build().ToByteArray(); break; } case Belief.BeliefType.BASE: { // Base Gpb_Base.Builder proto = Gpb_Base.CreateBuilder(); Belief_Base b = (Belief_Base)belief; proto.SetId(b.getId()); proto.SetSupplies(b.getSupplies()); // Copy contents of cell list List <GridCell> cells = b.getCells(); for (int i = 0; i < cells.Count; i++) { Gpb_GridCell.Builder g = Gpb_GridCell.CreateBuilder(); g.SetRow(cells[i].getRow()); g.SetCol(cells[i].getCol()); proto.AddCells(g.Build()); } // Add on belief time proto.SetBeliefTime(b.getBeliefTime()); // Form header + serialized message header = (byte)MessageType.BASE; body = proto.Build().ToByteArray(); break; } case Belief.BeliefType.CASUALTY_DELIVERY: { // Casualty Delivery Gpb_CasualtyDelivery.Builder proto = Gpb_CasualtyDelivery.CreateBuilder(); Belief_Casualty_Delivery b = (Belief_Casualty_Delivery)belief; proto.SetRequestTime(b.getRequest_time()); proto.SetActorId(b.getActor_id()); proto.SetGreedy(b.getGreedy()); proto.SetMultiplicity(b.getMultiplicity()); // Add on belief time proto.SetBeliefTime(b.getBeliefTime()); // Form header + serialized message header = (byte)MessageType.CASUALTY_DELIVERY; body = proto.Build().ToByteArray(); break; } case Belief.BeliefType.CASUALTY_PICKUP: { // Casualty Pickup Gpb_CasualtyPickup.Builder proto = Gpb_CasualtyPickup.CreateBuilder(); Belief_Casualty_Pickup b = (Belief_Casualty_Pickup)belief; proto.SetRequestTime(b.getRequest_time()); proto.SetActorId(b.getActor_id()); proto.SetGreedy(b.getGreedy()); // Copy contents of id list int[] ids = b.getIds(); for (int i = 0; i < ids.Length; i++) { proto.AddIds(ids[i]); } // Copy contents of multiplicity list int[] multiplicity = b.getMultiplicity(); for (int i = 0; i < multiplicity.Length; i++) { proto.AddMultiplicity(multiplicity[i]); } // Add on belief time proto.SetBeliefTime(b.getBeliefTime()); // Form header + serialized message header = (byte)MessageType.CASUALTY_PICKUP; body = proto.Build().ToByteArray(); break; } case Belief.BeliefType.GRIDSPEC: { // Grid Specification Gpb_GridSpec.Builder proto = Gpb_GridSpec.CreateBuilder(); Belief_GridSpec b = (Belief_GridSpec)belief; proto.SetWidth(b.getWidth()); proto.SetHeight(b.getHeight()); proto.SetGridOriginX(b.getGridOrigin_x()); proto.SetGridOriginZ(b.getGridOrigin_z()); proto.SetGridToWorldScale(b.getGridToWorldScale()); // Add on belief time proto.SetBeliefTime(b.getBeliefTime()); // Form header + serialized message header = (byte)MessageType.GRIDSPEC; body = proto.Build().ToByteArray(); break; } case Belief.BeliefType.MODE_COMMAND: { // Mode Command Gpb_Mode_Command.Builder proto = Gpb_Mode_Command.CreateBuilder(); Belief_Mode_Command b = (Belief_Mode_Command)belief; proto.SetRequestTime(b.getRequest_time()); proto.SetActorId(b.getActor_id()); proto.SetModeId(b.getMode_id()); // Add on belief time proto.SetBeliefTime(b.getBeliefTime()); // Form header + serialized message header = (byte)MessageType.MODE_COMMAND; body = proto.Build().ToByteArray(); break; } case Belief.BeliefType.NGOSITE: { // NGO Site Gpb_NGOSite.Builder proto = Gpb_NGOSite.CreateBuilder(); Belief_NGOSite b = (Belief_NGOSite)belief; proto.SetId(b.getId()); proto.SetSupplies(b.getSupplies()); proto.SetCasualties(b.getCasualties()); proto.SetCivilians(b.getCivilians()); // Copy contents of cell list List <GridCell> cells = b.getCells(); for (int i = 0; i < cells.Count; i++) { Gpb_GridCell.Builder g = Gpb_GridCell.CreateBuilder(); g.SetRow(cells[i].getRow()); g.SetCol(cells[i].getCol()); proto.AddCells(g.Build()); } // Add on belief time proto.SetBeliefTime(b.getBeliefTime()); // Form header + serialized message header = (byte)MessageType.NGOSITE; body = proto.Build().ToByteArray(); break; } case Belief.BeliefType.ROADCELL: { // Road Cell Gpb_RoadCell.Builder proto = Gpb_RoadCell.CreateBuilder(); Belief_RoadCell b = (Belief_RoadCell)belief; proto.SetIsRoadEnd(b.getIsRoadEnd()); // Copy contents of cell GridCell cell = b.getCell(); Gpb_GridCell.Builder g = Gpb_GridCell.CreateBuilder(); g.SetRow(cell.getRow()); g.SetCol(cell.getCol()); proto.SetCell(g.Build()); // Add on belief time proto.SetBeliefTime(b.getBeliefTime()); // Form header + serialized message header = (byte)MessageType.ROADCELL; body = proto.Build().ToByteArray(); break; } case Belief.BeliefType.SPOI: { // SPOI Gpb_SPOI.Builder proto = Gpb_SPOI.CreateBuilder(); Belief_SPOI b = (Belief_SPOI)belief; proto.SetRequestTime(b.getRequest_time()); proto.SetActorId(b.getActor_id()); proto.SetPosX(b.getPos_x()); proto.SetPosY(b.getPos_y()); proto.SetPosZ(b.getPos_z()); // Add on belief time proto.SetBeliefTime(b.getBeliefTime()); // Form header + serialized message header = (byte)MessageType.SPOI; body = proto.Build().ToByteArray(); break; } case Belief.BeliefType.SUPPLY_DELIVERY: { // Supply Delivery Gpb_SupplyDelivery.Builder proto = Gpb_SupplyDelivery.CreateBuilder(); Belief_Supply_Delivery b = (Belief_Supply_Delivery)belief; proto.SetRequestTime(b.getRequest_time()); proto.SetActorId(b.getActor_id()); proto.SetGreedy(b.getGreedy()); // Copy contents of id list int[] ids = b.getIds(); for (int i = 0; i < ids.Length; i++) { proto.AddIds(ids[i]); } // Copy contents of multiplicity list int[] multiplicity = b.getMultiplicity(); for (int i = 0; i < multiplicity.Length; i++) { proto.AddMultiplicity(multiplicity[i]); } // Add on belief time proto.SetBeliefTime(b.getBeliefTime()); // Form header + serialized message header = (byte)MessageType.SUPPLY_DELIVERY; body = proto.Build().ToByteArray(); break; } case Belief.BeliefType.SUPPLY_PICKUP: { // Supply Pickup Gpb_SupplyPickup.Builder proto = Gpb_SupplyPickup.CreateBuilder(); Belief_Supply_Pickup b = (Belief_Supply_Pickup)belief; proto.SetRequestTime(b.getRequest_time()); proto.SetActorId(b.getActor_id()); proto.SetGreedy(b.getGreedy()); proto.SetMultiplicity(b.getMultiplicity()); // Add on belief time proto.SetBeliefTime(b.getBeliefTime()); // Form header + serialized message header = (byte)MessageType.SUPPLY_PICKUP; body = proto.Build().ToByteArray(); break; } case Belief.BeliefType.TERRAIN: { // Terrain Gpb_Terrain.Builder proto = Gpb_Terrain.CreateBuilder(); Belief_Terrain b = (Belief_Terrain)belief; proto.SetType(b.getType()); // Copy contents of cell list List <GridCell> cells = b.getCells(); for (int i = 0; i < cells.Count; i++) { Gpb_GridCell.Builder g = Gpb_GridCell.CreateBuilder(); g.SetRow(cells[i].getRow()); g.SetCol(cells[i].getCol()); proto.AddCells(g.Build()); } // Add on belief time proto.SetBeliefTime(b.getBeliefTime()); // Form header + serialized message header = (byte)MessageType.TERRAIN; body = proto.Build().ToByteArray(); break; } case Belief.BeliefType.TIME: { // Time Gpb_Time.Builder proto = Gpb_Time.CreateBuilder(); Belief_Time b = (Belief_Time)belief; proto.SetTime(b.getTime()); // Add on belief time proto.SetBeliefTime(b.getBeliefTime()); // Form header + serialized message header = (byte)MessageType.TIME; body = proto.Build().ToByteArray(); break; } case Belief.BeliefType.VILLAGE: { // Village Gpb_Village.Builder proto = Gpb_Village.CreateBuilder(); Belief_Village b = (Belief_Village)belief; proto.SetId(b.getId()); proto.SetSupplies(b.getSupplies()); proto.SetCasualties(b.getCasualties()); // Copy contents of cell list List <GridCell> cells = b.getCells(); for (int i = 0; i < cells.Count; i++) { Gpb_GridCell.Builder g = Gpb_GridCell.CreateBuilder(); g.SetRow(cells[i].getRow()); g.SetCol(cells[i].getCol()); proto.AddCells(g.Build()); } // Add on belief time proto.SetBeliefTime(b.getBeliefTime()); // Form header + serialized message header = (byte)MessageType.VILLAGE; body = proto.Build().ToByteArray(); break; } case Belief.BeliefType.WAYPOINT: { // Waypoint Gpb_Waypoint.Builder proto = Gpb_Waypoint.CreateBuilder(); Belief_Waypoint b = (Belief_Waypoint)belief; proto.SetRequestTime(b.getRequest_time()); proto.SetActorId(b.getActor_id()); proto.SetPosX(b.getPos_x()); proto.SetPosY(b.getPos_y()); proto.SetPosZ(b.getPos_z()); proto.SetSpeed(b.getSpeed()); // Add on belief time proto.SetBeliefTime(b.getBeliefTime()); // Form header + serialized message header = (byte)MessageType.WAYPOINT; body = proto.Build().ToByteArray(); break; } case Belief.BeliefType.WAYPOINT_OVERRIDE: { // Waypoint Override Gpb_Waypoint_Override.Builder proto = Gpb_Waypoint_Override.CreateBuilder(); Belief_Waypoint_Override b = (Belief_Waypoint_Override)belief; proto.SetRequestTime(b.getRequest_time()); proto.SetActorId(b.getActor_id()); proto.SetPosX(b.getPos_x()); proto.SetPosY(b.getPos_y()); proto.SetPosZ(b.getPos_z()); // Add on belief time proto.SetBeliefTime(b.getBeliefTime()); // Form header + serialized message header = (byte)MessageType.WAYPOINT_OVERRIDE; body = proto.Build().ToByteArray(); break; } case Belief.BeliefType.WAYPOINT_PATH: { Gpb_WaypointPath.Builder proto = Gpb_WaypointPath.CreateBuilder(); Belief_WaypointPath b = (Belief_WaypointPath)belief; proto.SetActorId(b.getId()); proto.SetBeliefTime(b.getBeliefTime()); proto.SetRequestTime(b.getRequestTime()); foreach (Waypoint point in b.getWaypoints()) { Gpb_SingleWaypoint.Builder pointBuilder = Gpb_SingleWaypoint.CreateBuilder(); pointBuilder.SetX(point.x); pointBuilder.SetY(point.y); pointBuilder.SetZ(point.z); pointBuilder.SetHeading(point.heading); pointBuilder.SetVisited(point.visited); pointBuilder.SetSpeed(point.speed); proto.AddWaypoints(pointBuilder.Build()); } header = (byte)MessageType.WAYPOINT_PATH; body = proto.Build().ToByteArray(); break; } case Belief.BeliefType.CUSTOM: { // Custom Gpb_Custom.Builder proto = Gpb_Custom.CreateBuilder(); Belief_Custom b = (Belief_Custom)belief; proto.SetData(Google.ProtocolBuffers.ByteString.CopyFrom(b.getData())); // Add on belief time proto.SetCustomType(b.getCustomType()); proto.SetActorId(b.getId()); proto.SetBeliefTime(b.getBeliefTime()); // Form header + serialized message header = (byte)MessageType.CUSTOM; body = proto.Build().ToByteArray(); break; } default: // Unrecognized type, return empty array Log.debug("ProtobufSerializer.serializeBelief(): Unrecognized Belief type"); return(new byte[0]); } // Return serialized message (header + body) message = new Byte[body.Length + 1]; message[0] = header; System.Buffer.BlockCopy(body, 0, message, 1, body.Length); return(message); }