Guest User

EvilSensor Patch #5

a guest
Nov 11th, 2014
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 7.83 KB | None | 0 0
  1. --- EvilSensor.cs   2014-11-11 22:29:17.661597900 -0500
  2. +++ EvilSensor_new.cs   2014-11-11 21:09:43.166512400 -0500
  3. @@ -8,69 +8,116 @@
  4.      [MyEntityComponentDescriptor(typeof(MyObjectBuilder_SensorBlock))]
  5.      public class EvilSensor : MyGameLogicComponent
  6.      {
  7. +        // Initialize them variables
  8.          static System.String[] OreNames;
  9.  
  10.          IMySensorBlock Sensor;
  11.  
  12.          public override void Close()
  13.          {
  14. +            // State changed! I'm not sure how this part works since sensor_StateChanged  returns void? Maybe someone could enlighten me?
  15. +            // I'm probably confused by the use of the -= operator.
  16.              Sensor.StateChanged -= sensor_StateChanged;
  17.          }
  18.  
  19.          public override MyObjectBuilder_EntityBase GetObjectBuilder(bool copy = false)
  20.          {
  21. +            // This is a required override, but we return null and let the caller handle the exception
  22.              return null;
  23.          }
  24.  
  25.          public override void Init(MyObjectBuilder_EntityBase objectBuilder)
  26.          {
  27. +            // If OreNames is empty, get the String[] of names from one of our references
  28.              if (OreNames == null)
  29.              {
  30.                  MyDefinitionManager.Static.GetOreTypeNames(out OreNames);
  31.              }
  32. -
  33. +            // Define that Sensor is a SensorBloack
  34.              Sensor = Entity as IMySensorBlock;
  35. +            // State changed! I'm not sure how this part works since sensor_StateChanged  returns void? Maybe someone could enlighten me?
  36. +            // I'm probably confused by the use of the -= operator.
  37.              Sensor.StateChanged += sensor_StateChanged;
  38.          }
  39.  
  40.          void sensor_StateChanged(bool obj)
  41.          {
  42. -            if(!obj) return;
  43. +            // I think this part is depending on whether you entered or exited the range. !obj would be when you exited the range, so nothing happens.
  44. +            if (!obj) return;
  45.  
  46. +            // Define some mo' variables
  47.              string ore = null;
  48.  
  49. +            string sensorName = Sensor.CustomName.ToLowerInvariant();
  50. +
  51.              var oreAmount = 100;
  52.  
  53. -            foreach(var o in OreNames)
  54. +            var distance = 1.5f;
  55. +
  56. +            string dataString = "";
  57. +
  58. +            //Loop through the ore names in the array and see if the sensor's name contains one of them
  59. +            foreach (var o in OreNames)
  60.              {
  61. -                if (Sensor.CustomName.StartsWith(o, System.StringComparison.InvariantCultureIgnoreCase))
  62. +                if (sensorName.Contains(o.ToLowerInvariant()))
  63.                  {
  64.                      ore = o;
  65. -                    try
  66. -                    {
  67. -                        oreAmount = System.Convert.ToInt32(Sensor.CustomName.Substring(Sensor.CustomName.LastIndexOf(" ") + 1));
  68. -                    }
  69. -                    catch (System.FormatException)
  70. -                    {
  71. -                        oreAmount = 100;
  72. -                    }
  73.                      break;
  74.                  }
  75.              }
  76. -
  77. +            // If the sensor name does not contain an ore name, we exit here
  78.              if (ore == null)
  79.                  return;
  80. -
  81. -            // We want to spawn ore and throw it at entity which entered sensor
  82. +            // See if the sensor name contains the "data string" as defined  by beginning with "[" and ending with "]"
  83. +            if (sensorName.Contains("[") && sensorName.Contains("]"))
  84. +            {
  85. +                /*
  86. +                 * At this point, we parse the "data string" from the name,
  87. +                 * check to see what variables it contains and their order, then we parse our data from it.
  88. +                */
  89. +                try
  90. +                {
  91. +                dataString = sensorName.Slice(sensorName.IndexOf("[") + 1, sensorName.LastIndexOf("]")).ToLowerInvariant().Replace(" ", "");
  92. +                if (dataString.Contains("amt:") && dataString.Contains("dst:"))
  93. +                {
  94. +                    if (dataString.IndexOf("amt:") < dataString.IndexOf("dst:")) {
  95. +                        oreAmount = System.Convert.ToInt32(dataString.Slice(dataString.IndexOf(":") + 1, dataString.IndexOf(";")));;
  96. +                        distance = System.Convert.ToSingle(dataString.Substring(dataString.LastIndexOf(":") + 1));
  97. +                    } else {
  98. +                        distance = System.Convert.ToSingle(dataString.Slice(dataString.IndexOf(":") + 1, dataString.IndexOf(";")));
  99. +                        oreAmount = System.Convert.ToInt32(dataString.Substring(dataString.LastIndexOf(":") + 1));
  100. +                    }
  101. +                }
  102. +                else if (dataString.Contains("amt:"))
  103. +                {
  104. +                    oreAmount = System.Convert.ToInt32(dataString.Substring(dataString.LastIndexOf(":") + 1));
  105. +                }
  106. +                else if (dataString.Contains("dst:"))
  107. +                {
  108. +                    distance = System.Convert.ToSingle(dataString.Substring(dataString.LastIndexOf(":") + 1));
  109. +                }
  110. +                
  111. +                }
  112. +                catch (System.Exception)
  113. +                {
  114. +                    // If something got fubar'ed, set the defaults and try to continue without crashing the damn game
  115. +                    oreAmount = 100;
  116. +                    distance = 1.5f;
  117. +                }
  118. +            }
  119. +            // Create the object to be spawned
  120.              MyObjectBuilder_FloatingObject floatingBuilder = new MyObjectBuilder_FloatingObject();
  121. +            // Specify that the object is an ore inventory item of the amount & type specified by the user
  122.              floatingBuilder.Item = new MyObjectBuilder_InventoryItem() { Amount = oreAmount, Content = new MyObjectBuilder_Ore() { SubtypeName = ore } };
  123. -            floatingBuilder.PersistentFlags = MyPersistentEntityFlags2.InScene; // Very important
  124. -            floatingBuilder.PositionAndOrientation = new MyPositionAndOrientation(Sensor.WorldMatrix.Translation + Sensor.WorldMatrix.Forward * 1.5f, Sensor.WorldMatrix.Forward, Sensor.WorldMatrix.Up);
  125. +            floatingBuilder.PersistentFlags = MyPersistentEntityFlags2.InScene; // Very important, but I'm not sure why. I guess if not flagged as InScene, it won't be recognized as actually being there would be my guess.
  126. +            // Set the postition and orientation of the object in the world.
  127. +            floatingBuilder.PositionAndOrientation = new MyPositionAndOrientation(Sensor.WorldMatrix.Translation + Sensor.WorldMatrix.Forward * distance, Sensor.WorldMatrix.Forward, Sensor.WorldMatrix.Up);
  128. +            // Check to make sure this is a server instance of the mod (I think it runs on client as well), otherwise a duplicate, non-functional object will be spawned by the client as well. (Duplicate bug)
  129.              if (Sandbox.ModAPI.MyAPIGateway.Multiplayer.IsServer)
  130.              {
  131. +                // Finally, we actually create & spawn the damn thing in the world here. Enjoy!
  132.                  var floatingObject = Sandbox.ModAPI.MyAPIGateway.Entities.CreateFromObjectBuilderAndAdd(floatingBuilder);
  133.              }
  134. -            // Now it only creates ore, we will throw it later
  135.          }
  136.  
  137.          public override void MarkForClose()
  138. @@ -105,4 +152,21 @@
  139.          {
  140.          }
  141.      }
  142. +
  143. +    public static class Extensions
  144. +    {
  145. +        /// <summary>
  146. +        /// Get the string slice between the two indexes.
  147. +        /// Inclusive for start index, exclusive for end index.
  148. +        /// </summary>
  149. +        public static string Slice(this string source, int start, int end)
  150. +        {
  151. +            if (end < 0) // Keep this for negative end support
  152. +            {
  153. +                end = source.Length + end;
  154. +            }
  155. +            int len = end - start;               // Calculate length
  156. +            return source.Substring(start, len); // Return Substring of length
  157. +        }
  158. +    }
  159.  }
Advertisement
Add Comment
Please, Sign In to add comment