Guest User

EvilSensor Patch #6

a guest
Nov 13th, 2014
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 15.19 KB | None | 0 0
  1. --- EvilSensor.cs   2014-11-11 21:10:20.000000000 -0500
  2. +++ EvilSensor_new.cs   2014-11-13 23:11:04.698537600 -0500
  3. @@ -1,28 +1,32 @@
  4.  using Sandbox.Common.Components;
  5.  using Sandbox.Common.ObjectBuilders;
  6.  using Sandbox.Definitions;
  7. +using Sandbox.ModAPI;
  8.  using Sandbox.ModAPI.Ingame;
  9. +using System;
  10.  
  11. -namespace EvilSensor
  12. +namespace EvilSensor_1
  13.  {
  14.      [MyEntityComponentDescriptor(typeof(MyObjectBuilder_SensorBlock))]
  15.      public class EvilSensor : MyGameLogicComponent
  16.      {
  17.          // Initialize them variables
  18. -        static System.String[] OreNames;
  19. +        static String[] OreNames;
  20.  
  21.          IMySensorBlock Sensor;
  22.  
  23.          public override void Close()
  24.          {
  25. -            // State changed! I'm not sure how this part works since sensor_StateChanged  returns void? Maybe someone could enlighten me?
  26. -            // I'm probably confused by the use of the -= operator.
  27. +            /* State changed! I'm not sure how this part works since sensor_StateChanged  returns void?
  28. +             * Maybe someone could enlighten me?
  29. +             * I'm probably confused by the use of the -= operator.
  30. +             */
  31.              Sensor.StateChanged -= sensor_StateChanged;
  32.          }
  33.  
  34.          public override MyObjectBuilder_EntityBase GetObjectBuilder(bool copy = false)
  35.          {
  36. -            // This is a required override, but we return null and let the caller handle the exception
  37. +            // This is a required override, but we return null and let the caller handle null
  38.              return null;
  39.          }
  40.  
  41. @@ -35,14 +39,17 @@
  42.              }
  43.              // Define that Sensor is a SensorBloack
  44.              Sensor = Entity as IMySensorBlock;
  45. -            // State changed! I'm not sure how this part works since sensor_StateChanged  returns void? Maybe someone could enlighten me?
  46. -            // I'm probably confused by the use of the -= operator.
  47. +            /* State changed! I'm not sure how this part works since sensor_StateChanged  returns void?
  48. +             * Maybe someone could enlighten me?
  49. +             * I'm probably confused by the use of the += operator.
  50. +             */
  51.              Sensor.StateChanged += sensor_StateChanged;
  52.          }
  53.  
  54.          void sensor_StateChanged(bool obj)
  55.          {
  56. -            // 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.
  57. +            // I think this part is depending on whether you entered or exited the range.
  58. +            // !obj would be when you exited the range, so nothing happens.
  59.              if (!obj) return;
  60.  
  61.              // Define some mo' variables
  62. @@ -50,6 +57,8 @@
  63.  
  64.              string sensorName = Sensor.CustomName.ToLowerInvariant();
  65.  
  66. +            VRageMath.Vector3 direction = Sensor.WorldMatrix.Forward;
  67. +
  68.              var oreAmount = 100;
  69.  
  70.              var distance = 1.5f;
  71. @@ -68,55 +77,160 @@
  72.              // If the sensor name does not contain an ore name, we exit here
  73.              if (ore == null)
  74.                  return;
  75. -            // See if the sensor name contains the "data string" as defined  by beginning with "[" and ending with "]"
  76. +            // See if the sensor name contains the "data string" as defined
  77. +            // by beginning with "[" and ending with "]"
  78.              if (sensorName.Contains("[") && sensorName.Contains("]"))
  79.              {
  80.                  /*
  81.                   * At this point, we parse the "data string" from the name,
  82. -                 * check to see what variables it contains and their order, then we parse our data from it.
  83. +                 * break it down into sections, see what tags those sections
  84. +                 * contain, then parse our data from it
  85.                  */
  86.                  try
  87.                  {
  88. -                dataString = sensorName.Slice(sensorName.IndexOf("[") + 1, sensorName.LastIndexOf("]")).ToLowerInvariant().Replace(" ", "");
  89. -                if (dataString.Contains("amt:") && dataString.Contains("dst:"))
  90. -                {
  91. -                    if (dataString.IndexOf("amt:") < dataString.IndexOf("dst:")) {
  92. -                        oreAmount = System.Convert.ToInt32(dataString.Slice(dataString.IndexOf(":") + 1, dataString.IndexOf(";")));;
  93. -                        distance = System.Convert.ToSingle(dataString.Substring(dataString.LastIndexOf(":") + 1));
  94. -                    } else {
  95. -                        distance = System.Convert.ToSingle(dataString.Slice(dataString.IndexOf(":") + 1, dataString.IndexOf(";")));
  96. -                        oreAmount = System.Convert.ToInt32(dataString.Substring(dataString.LastIndexOf(":") + 1));
  97. +                    dataString = sensorName.Slice(sensorName.IndexOf("[") + 1, sensorName.LastIndexOf("]")).ToLowerInvariant().Replace(" ", "");
  98. +                    string dataSection;
  99. +                    bool control = true;
  100. +                    while (control)
  101. +                    {
  102. +                        if (dataString.Contains(";"))
  103. +                        {
  104. +                            dataSection = dataString.Slice(0, dataString.IndexOf(";"));
  105. +                            dataString = dataString.Substring(dataString.IndexOf(";") + 1);
  106. +                            string tag = dataSection.Slice(0, dataSection.IndexOf(":") + 1);
  107. +                            string data = dataSection.Substring(dataSection.IndexOf(":") + 1);
  108. +                            switch (tag)
  109. +                            {
  110. +                                case "amt:":
  111. +                                    oreAmount = Convert.ToInt32(data);
  112. +                                    break;
  113. +                                case "dst:":
  114. +                                    distance = Convert.ToSingle(data);
  115. +                                    break;
  116. +                                case "dir:":
  117. +                                    switch (data)
  118. +                                    {
  119. +                                        case "up":
  120. +                                            direction = Sensor.WorldMatrix.Up;
  121. +                                            break;
  122. +                                        case "down":
  123. +                                            direction = Sensor.WorldMatrix.Down;
  124. +                                            break;
  125. +                                        case "left":
  126. +                                            direction = Sensor.WorldMatrix.Left;
  127. +                                            break;
  128. +                                        case "right":
  129. +                                            direction = Sensor.WorldMatrix.Right;
  130. +                                            break;
  131. +                                        case "front":
  132. +                                            direction = Sensor.WorldMatrix.Forward;
  133. +                                            break;
  134. +                                        case "back":
  135. +                                            direction = Sensor.WorldMatrix.Backward;
  136. +                                            break;
  137. +                                        default:
  138. +                                            direction = Sensor.WorldMatrix.Forward;
  139. +                                            break;
  140. +                                    }
  141. +                                    break;
  142. +                                default:
  143. +                                    break;
  144. +                            }
  145. +                        }
  146. +                        else
  147. +                        {
  148. +                            dataSection = dataString;
  149. +                            string tag = dataSection.Slice(0, dataSection.IndexOf(":") + 1);
  150. +                            string data = dataSection.Substring(dataSection.IndexOf(":") + 1);
  151. +                            switch (tag)
  152. +                            {
  153. +                                case "amt:":
  154. +                                    oreAmount = Convert.ToInt32(data);
  155. +                                    break;
  156. +                                case "dst:":
  157. +                                    distance = Convert.ToSingle(data);
  158. +                                    break;
  159. +                                case "dir:":
  160. +                                    switch (data)
  161. +                                    {
  162. +                                        case "up":
  163. +                                            direction = Sensor.WorldMatrix.Up;
  164. +                                            break;
  165. +                                        case "down":
  166. +                                            direction = Sensor.WorldMatrix.Down;
  167. +                                            break;
  168. +                                        case "left":
  169. +                                            direction = Sensor.WorldMatrix.Left;
  170. +                                            break;
  171. +                                        case "right":
  172. +                                            direction = Sensor.WorldMatrix.Right;
  173. +                                            break;
  174. +                                        case "front":
  175. +                                            direction = Sensor.WorldMatrix.Forward;
  176. +                                            break;
  177. +                                        case "back":
  178. +                                            direction = Sensor.WorldMatrix.Backward;
  179. +                                            break;
  180. +                                        default:
  181. +                                            direction = Sensor.WorldMatrix.Forward;
  182. +                                            break;
  183. +                                    }
  184. +                                    break;
  185. +                                default:
  186. +                                    break;
  187. +                            }
  188. +                            control = false;
  189. +                        }
  190.                      }
  191. +                    /* Old string handling
  192. +                    if (dataString.Contains("amt:") && dataString.Contains("dst:"))
  193. +                    {
  194. +                        if (dataString.IndexOf("amt:") < dataString.IndexOf("dst:")) {
  195. +                            oreAmount = Convert.ToInt32(dataString.Slice(dataString.IndexOf(":") + 1, dataString.IndexOf(";")));;
  196. +                            distance = Convert.ToSingle(dataString.Substring(dataString.LastIndexOf(":") + 1));
  197. +                        } else {
  198. +                            distance = Convert.ToSingle(dataString.Slice(dataString.IndexOf(":") + 1, dataString.IndexOf(";")));
  199. +                            oreAmount = Convert.ToInt32(dataString.Substring(dataString.LastIndexOf(":") + 1));
  200. +                        }
  201. +                    }
  202. +                    else if (dataString.Contains("amt:"))
  203. +                    {
  204. +                        oreAmount = Convert.ToInt32(dataString.Substring(dataString.LastIndexOf(":") + 1));
  205. +                    }
  206. +                    else if (dataString.Contains("dst:"))
  207. +                    {
  208. +                        distance = Convert.ToSingle(dataString.Substring(dataString.LastIndexOf(":") + 1));
  209. +                    }
  210. +                    */
  211.                  }
  212. -                else if (dataString.Contains("amt:"))
  213. -                {
  214. -                    oreAmount = System.Convert.ToInt32(dataString.Substring(dataString.LastIndexOf(":") + 1));
  215. -                }
  216. -                else if (dataString.Contains("dst:"))
  217. -                {
  218. -                    distance = System.Convert.ToSingle(dataString.Substring(dataString.LastIndexOf(":") + 1));
  219. -                }
  220. -                
  221. -                }
  222. -                catch (System.Exception)
  223. +                catch (Exception ex)
  224.                  {
  225.                      // If something got fubar'ed, set the defaults and try to continue without crashing the damn game
  226.                      oreAmount = 100;
  227.                      distance = 1.5f;
  228. +                    direction = Sensor.WorldMatrix.Forward;
  229. +                    Logging.Instance.WriteLine(String.Format("sensor_StateChanged(), String Parser: {0}", ex.ToString()));
  230. +                }
  231. +            }
  232. +            try
  233. +            {
  234. +                // Create the object to be spawned
  235. +                MyObjectBuilder_FloatingObject floatingBuilder = new MyObjectBuilder_FloatingObject();
  236. +                // Specify that the object is an ore inventory item of the amount & type specified by the user
  237. +                floatingBuilder.Item = new MyObjectBuilder_InventoryItem() { Amount = oreAmount, Content = new MyObjectBuilder_Ore() { SubtypeName = ore } };
  238. +                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.
  239. +                // Set the postition and orientation of the object in the world.
  240. +                floatingBuilder.PositionAndOrientation = new MyPositionAndOrientation(Sensor.WorldMatrix.Translation + direction * distance, Sensor.WorldMatrix.Forward, Sensor.WorldMatrix.Up);
  241. +                // 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)
  242. +                if (Sandbox.ModAPI.MyAPIGateway.Multiplayer.IsServer)
  243. +                {
  244. +                    // Finally, we actually create & spawn the damn thing in the world here. Enjoy!
  245. +                    var floatingObject = Sandbox.ModAPI.MyAPIGateway.Entities.CreateFromObjectBuilderAndAdd(floatingBuilder);
  246.                  }
  247.              }
  248. -            // Create the object to be spawned
  249. -            MyObjectBuilder_FloatingObject floatingBuilder = new MyObjectBuilder_FloatingObject();
  250. -            // Specify that the object is an ore inventory item of the amount & type specified by the user
  251. -            floatingBuilder.Item = new MyObjectBuilder_InventoryItem() { Amount = oreAmount, Content = new MyObjectBuilder_Ore() { SubtypeName = ore } };
  252. -            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.
  253. -            // Set the postition and orientation of the object in the world.
  254. -            floatingBuilder.PositionAndOrientation = new MyPositionAndOrientation(Sensor.WorldMatrix.Translation + Sensor.WorldMatrix.Forward * distance, Sensor.WorldMatrix.Forward, Sensor.WorldMatrix.Up);
  255. -            // 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)
  256. -            if (Sandbox.ModAPI.MyAPIGateway.Multiplayer.IsServer)
  257. +            catch (Exception ex)
  258.              {
  259. -                // Finally, we actually create & spawn the damn thing in the world here. Enjoy!
  260. -                var floatingObject = Sandbox.ModAPI.MyAPIGateway.Entities.CreateFromObjectBuilderAndAdd(floatingBuilder);
  261. +                Logging.Instance.WriteLine(String.Format("sensor_StateChanged(), Object Builder: {0}", ex.ToString()));
  262.              }
  263.          }
Advertisement
Add Comment
Please, Sign In to add comment