PsyOps

CustomGreens.cs

Nov 3rd, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using BattleCore.Events;
  7. using BattleCorePsyOps;
  8.  
  9. using System.IO;
  10.  
  11. namespace BattleZone
  12. {
  13.     public class GreenControl
  14.     {
  15.         // My custom chat mod
  16.         ShortChat msg = new ShortChat();
  17.         // Use this for random numbers
  18.         private Random ran = new Random();
  19.        
  20.         // List of all the active green indexes- a list of all active greens.
  21.         private List<int> _ActiveGreenIndex = new List<int>();
  22.         // All greens and green status - if we activate put the index into _ActiveGreenIndex
  23.         private List<CustomGreen> _GreenMasterList = new List<CustomGreen>();
  24.        
  25.         // Each cycle this will tell use if we have to send another gfx command
  26.         private bool _needUpdateLvz = false;
  27.         // This is the gfc command string
  28.         private string _toggleGreenz = "*objset ";
  29.         // We stack them into this q
  30.         private Queue<EventArgs> _CommandUpdates = new Queue<EventArgs>();
  31.        
  32.         // How many active greens we want out at a time
  33.         private byte _ActiveGreens = 5;
  34.  
  35.         // Use this for debug
  36.         public void Test()
  37.         {    turnRandomGreenOn(false);   }
  38.  
  39.         // Add greens to the master list - this comes from cfg file
  40.         private void RegisterGreen(ushort LvzStartIndex, ushort x, ushort y, byte radius)
  41.         {    _GreenMasterList.Add(new CustomGreen(LvzStartIndex, x, y, radius));    }
  42.  
  43.         // This is a timed event - updates all expired greens
  44.         // and returns all toggled lvz commands. Objset
  45.         public Queue<EventArgs> UpdateGreenGfx
  46.         {
  47.             get{
  48.                 // Make sure cfg was loaded
  49.                 if (_GreenMasterList.Count > 0)
  50.                 {
  51.                     // Check expired greens
  52.                     for (int i = 0; i < _ActiveGreenIndex.Count; i += 1)
  53.                     {
  54.                         int index = _ActiveGreenIndex[i];
  55.                         if (_GreenMasterList[index].GreenExpired)
  56.                             turnGreenOff(i, index);
  57.                     }
  58.  
  59.                     // Fill greens to active green count
  60.                     if (_ActiveGreenIndex.Count < _ActiveGreens)
  61.                         turnRandomGreenOn(true);
  62.  
  63.                     // Dump the command into chat q
  64.                     if (_needUpdateLvz)
  65.                     {
  66.                         _CommandUpdates.Enqueue(msg.pub(_toggleGreenz));
  67.                         _toggleGreenz = "*objset ";
  68.                         _needUpdateLvz = false;
  69.                     }
  70.  
  71.                     // Send the chat q
  72.                     if (_CommandUpdates.Count > 0)
  73.                         return _CommandUpdates;
  74.                 }
  75.  
  76.                 return null;
  77.             }
  78.         }
  79.         // All required tasks to turn a single green off. onPlayerGetGreen or expired time
  80.         private void turnGreenOff(int GIndex, int index)
  81.         {
  82.             _GreenMasterList[index].Active = false;
  83.             _toggleGreenz += "-" + (_GreenMasterList[index].LvzID + GetPrizeID(_GreenMasterList[index].Type)) + ",";
  84.             _ActiveGreenIndex.RemoveAt(GIndex);
  85.             _needUpdateLvz = true;
  86.         }
  87.  
  88.         private int _NextRegen = 10;
  89.         private DateTime _RegenTimeStamp = DateTime.Now;
  90.  
  91.         // All required tasks to turn on a random green on
  92.         private void turnRandomGreenOn( bool RanTime)
  93.         {
  94.             if (RanTime && (DateTime.Now - _RegenTimeStamp).TotalSeconds >= _NextRegen || !RanTime)
  95.             {
  96.                 _RegenTimeStamp = DateTime.Now;
  97.                 _NextRegen = ran.Next(1, 30);
  98.                 turnGreenOn();
  99.             }
  100.         }
  101.         // Turn on random green - random prize - random time
  102.         private void turnGreenOn()
  103.         {
  104.             bool searching = true;
  105.             while (searching)
  106.             {
  107.                 // making sure i dont create an infinite loop if all greens have been spawned
  108.                 if (_ActiveGreenIndex.Count >= _GreenMasterList.Count) searching = false;
  109.  
  110.                 // get a random number for green spawn
  111.                 int index = ran.Next(0, _GreenMasterList.Count);
  112.  
  113.                 // checking if the green hasnt been already activated
  114.                 if (!_GreenMasterList[index].Active)
  115.                 {
  116.                     turnGreenOn(index);
  117.                     searching = false;
  118.                 }
  119.             }
  120.         }
  121.         // Turn on green - random prize
  122.         private void turnGreenOn(int index)
  123.         {
  124.             turnGreenOn(index, ran.Next(0, 11));
  125.         }
  126.         // Turn on specific green
  127.         private void turnGreenOn(int index, int PrizeIndex)
  128.         {
  129.             _GreenMasterList[index].Active = true;
  130.             _GreenMasterList[index].Type = PrizeLvzIDList[PrizeIndex];
  131.             _ActiveGreenIndex.Add(index);
  132.             _toggleGreenz += "+" + (_GreenMasterList[index].LvzID + GetPrizeID(_GreenMasterList[index].Type)) + ",";
  133.             _GreenMasterList[index].SetTimeStamp();
  134.             _GreenMasterList[index].SetAliveTime(ran.Next(20,100));
  135.             _needUpdateLvz = true;
  136.         }
  137.  
  138.         // Basically collision detection on greens and players
  139.         public void CheckGreenRegions(PlayerPositionEvent p)
  140.         {
  141.             if (_ActiveGreenIndex.Count > 0)
  142.                 for (int i = 0; i < _ActiveGreenIndex.Count; i += 1)
  143.                     if (InHotSpot(p, _GreenMasterList[_ActiveGreenIndex[i]].HotSpot))
  144.                     {
  145.                         int index = _ActiveGreenIndex[i];
  146.                         turnGreenOff(i,index);
  147.                         if (_GreenMasterList[index].Type != PrizeType.Multi)
  148.                         {
  149.                             _CommandUpdates.Enqueue(msg.pm(p.PlayerName, "*prize #" + ((byte)_GreenMasterList[index].Type + 0)));
  150.                         }
  151.                         else
  152.                             _CommandUpdates.Enqueue(msg.pm(p.PlayerName, "*prize 500"));
  153.                         return;
  154.                     }
  155.         }
  156.  
  157.         // Collision detection utility method
  158.         private bool InHotSpot(PlayerPositionEvent Player, ushort[] HotSpot)
  159.         {
  160.             if (Player.MapPositionX > HotSpot[0] && Player.MapPositionX < HotSpot[2]
  161.                 && Player.MapPositionY > HotSpot[1] && Player.MapPositionY < HotSpot[3])
  162.                 return true;
  163.             return false;
  164.         }
  165.  
  166.         // These 2 methods help me return the index which is = to lvz ID numbers.
  167.         // Add this to the lvz id start index and you have your lvz ID for that prize.
  168.         private PrizeType[] PrizeLvzIDList = new PrizeType[12]
  169.         {
  170.             PrizeType.FullCharge,   PrizeType.EngineShutDown, PrizeType.Super,  PrizeType.Shields,
  171.             PrizeType.Repel,        PrizeType.Burst,          PrizeType.Decoy,  PrizeType.Thor,
  172.             PrizeType.Multi,        PrizeType.Brick,          PrizeType.Rocket, PrizeType.Portal,
  173.         };
  174.         private int GetPrizeID(PrizeType prize)
  175.         {
  176.             for (int i = 0; i < PrizeLvzIDList.Length; i += 1)
  177.                 if (prize == PrizeLvzIDList[i]) return i;
  178.             return 0;
  179.         }
  180.  
  181.         // Green object that holds all the info required to spawn and toggle lvz
  182.         private class CustomGreen
  183.         {
  184.             //  Creates the green and hotspot needed for collision
  185.             public CustomGreen(ushort LvzID, ushort xCoord, ushort yCoord, byte Radius)
  186.             {
  187.                 this._LvzID = LvzID;
  188.                 this._HotSpot = new ushort[4]
  189.                 {
  190.                     (ushort)(xCoord - Radius),
  191.                     (ushort)(yCoord - Radius),
  192.                     (ushort)(xCoord + Radius),
  193.                     (ushort)(yCoord + Radius)
  194.                 };
  195.             }
  196.  
  197.             // Lvz start id index
  198.             // Add the prize index to this and you have ID for that green and that prize
  199.             private ushort _LvzID;
  200.             public ushort LvzID
  201.             {    get { return _LvzID; } }
  202.  
  203.             // if green is active or not
  204.             private bool _Active = false;
  205.             public bool Active
  206.             {
  207.                 get { return _Active; }
  208.                 set { _Active = value; }
  209.             }
  210.  
  211.             // These are used to create timestamps and keep track of green alive times
  212.             private int _ExpireTime = 10;
  213.             public int ExpireTime
  214.             { get { return _ExpireTime; } }
  215.             private DateTime _TimeStamp = DateTime.Now;
  216.             public DateTime TimeStamp { get { return _TimeStamp; } }
  217.             public void SetAliveTime(int ExpireTime)
  218.             {    _ExpireTime = ExpireTime;   }
  219.             public void SetTimeStamp()
  220.             { _TimeStamp = DateTime.Now; }
  221.             public bool GreenExpired
  222.             {
  223.                 get
  224.                 {
  225.                     if ((DateTime.Now - _TimeStamp).TotalSeconds >= _ExpireTime)
  226.                         return true;
  227.                     return false;
  228.                 }
  229.             }
  230.  
  231.             // What prize you want to set for a green
  232.             private PrizeType _Type = PrizeType.FullCharge;
  233.             public PrizeType Type
  234.             {
  235.                 get { return _Type; }
  236.                 set { _Type = value; }
  237.             }
  238.  
  239.             // Hotspots for the greens
  240.             private ushort[] _HotSpot = new ushort[4];
  241.             public ushort[] HotSpot { get { return _HotSpot; } }
  242.         }
  243.  
  244.         // -------------------------------------- CFG get setting code  //
  245.         // ----------------------------------------------------------- //
  246.         private Queue<EventArgs> _cfgPrintOut = new Queue<EventArgs>();
  247.         public Queue<EventArgs> CfgPrintout
  248.         { get { return _cfgPrintOut; } }
  249.  
  250.         public void LoadCFG(string BotName)
  251.         {
  252.             if (File.Exists(BotName + "/customgreens.cfg"))
  253.             {
  254.                 try
  255.                 {
  256.                     //Opening appropriate file
  257.                     StreamReader SR = File.OpenText(BotName + "/customgreens.cfg");
  258.                     //used to read each line
  259.                     string S;
  260.  
  261.                     int AmountOfPrizes = 12;
  262.  
  263.                     bool MakeIni = false;
  264.                     string Lvzfilename = "";
  265.                     string[] iniSettings = new string[12];
  266.                     int[] LvzDimensions = new int[12];
  267.                     int DimIndex = 0;
  268.                     int IniIndex = 0;
  269.                     ushort[] GreenArea = new ushort[4];
  270.                     int XRowCol = 0;
  271.                     int YRowCol = 0;
  272.  
  273.                     // We will store the INI file in here line by line
  274.                     // and print it out later for file creation
  275.                     Queue<string> SavedIni = new Queue<string>();
  276.  
  277.                     // Extract and save all info from the cfg file
  278.                     while ((S = SR.ReadLine()) != null)
  279.                     {
  280.                         if (!S.StartsWith(";"))
  281.                         {
  282.                             string[] data = S.Trim().Split(':');
  283.  
  284.                             if (S.StartsWith("createini:"))
  285.                             {
  286.                                 if (data[1].ToLower().Equals("yes"))
  287.                                     MakeIni = true;
  288.                                 _cfgPrintOut.Enqueue(msg.arena("MakeIni set to [" + MakeIni + "]"));
  289.                             }
  290.                             else if (S.StartsWith("lvzfilename:"))
  291.                                 Lvzfilename = data[1];
  292.                             else if (S.StartsWith("FileInfo:"))
  293.                                 iniSettings[IniIndex++] = S;
  294.                             else if (S.StartsWith("Prize:"))
  295.                             {
  296.                                 LvzDimensions[DimIndex++] = int.Parse(data[2]);
  297.                             }
  298.                             else if (S.StartsWith("spawnarea:"))
  299.                             {
  300.                                 GreenArea[0] = (ushort)(int.Parse(data[1]) * 16);
  301.                                 GreenArea[1] = (ushort)(int.Parse(data[2]) * 16);
  302.                                 GreenArea[2] = (ushort)(int.Parse(data[3]) * 16);
  303.                                 GreenArea[3] = (ushort)(int.Parse(data[4]) * 16);
  304.                                 XRowCol = int.Parse(data[5]);
  305.                                 YRowCol = int.Parse(data[6]);
  306.                             }
  307.                         }
  308.                     }
  309.  
  310.                     // Closing file
  311.                     SR.Close();
  312.  
  313.                     // Save the interval we want set each green
  314.                     int xGap = (int)Math.Floor((double)((GreenArea[2] - GreenArea[0]) / XRowCol));
  315.                     int yGap = (int)Math.Floor((double)((GreenArea[3] - GreenArea[1]) / YRowCol));
  316.  
  317.                     // Start x and y coords of the green area
  318.                     int NextX = GreenArea[0];
  319.                     int NextY = GreenArea[1];
  320.  
  321.                     if (MakeIni)
  322.                     {
  323.                         // ----------------------------------------------- Ini generate file
  324.                         // Filename
  325.                         SavedIni.Enqueue("Outfile=" + Lvzfilename);
  326.                         // File listing
  327.                         for (int i = 0; i < AmountOfPrizes; i += 1)
  328.                             SavedIni.Enqueue("File=" + iniSettings[i].Split(':')[1]);
  329.                         // Object image listing
  330.                         SavedIni.Enqueue("[objectimages]");
  331.                         for (int i = 0; i < AmountOfPrizes; i += 1)
  332.                             SavedIni.Enqueue("IMAGE" + i + "=" + iniSettings[i].Split(':')[1] + "," + iniSettings[i].Split(':')[2]);
  333.                         // Map Objects title
  334.                         SavedIni.Enqueue("[mapobjects]");
  335.                     }
  336.  
  337.                     int IDCount = 0;
  338.  
  339.                     // Loop and position all greens
  340.                     // Create the green list
  341.                     // If setting is on Create the ini file as well
  342.                     for (int i = 0; i < XRowCol; i += 1)
  343.                     {
  344.                         int xCoor = NextX;
  345.                         NextX += xGap;
  346.  
  347.                         for (int j = 0; j < YRowCol; j += 1)
  348.                         {
  349.                             // reset Y once it gets to end
  350.                             if (NextY > GreenArea[3])
  351.                                 NextY = GreenArea[1];
  352.  
  353.                             // Increment the interval that we assigned
  354.                             int yCoor = NextY;
  355.                             NextY += yGap;
  356.  
  357.                             // ---------------Create the greens in here--------------- \\
  358.                             RegisterGreen((ushort)IDCount, (ushort)xCoor, (ushort)yCoor, 32);
  359.  
  360.                             for (int k = 0; k < AmountOfPrizes; k += 1)
  361.                             {
  362.                                 int LvzID = IDCount++;
  363.                                 if (MakeIni)
  364.                                     SavedIni.Enqueue((xCoor - LvzDimensions[k] / 2) + "," +
  365.                                         (yCoor - LvzDimensions[k] / 2) + ",IMAGE" + k + ",AfterShips,ServerControlled,0," + LvzID);
  366.                             }
  367.                         }
  368.                     }
  369.                     _cfgPrintOut.Enqueue(msg.arena("Greens created: [" + _GreenMasterList.Count + "]."));
  370.                     if (MakeIni)
  371.                     {
  372.                         //CustomGreenLVZ
  373.                         using (StreamWriter sw = new StreamWriter("lvz/customgreens/CustomGreenLVZ.ini"))
  374.                         {
  375.                             while (SavedIni.Count > 0)
  376.                                 sw.WriteLine(SavedIni.Dequeue());
  377.                         }
  378.  
  379.                         _cfgPrintOut.Enqueue(msg.arena("Ini successfuly created."));
  380.                         return;
  381.                     }
  382.                     else
  383.                         _cfgPrintOut.Enqueue(msg.arena("Create Ini: Off. Ini not created."));
  384.                     return;
  385.                 }
  386.                 catch (Exception ex) { _cfgPrintOut.Enqueue(msg.arena("Error:" + ex)); return;}
  387.             }
  388.             _cfgPrintOut.Enqueue(msg.arena("File not found: Ini not created."));
  389.         }
  390.  
  391.     }
  392.  
  393.     // Prizes and their *prize #s
  394.     public enum PrizeType
  395.     {
  396.         FullCharge = 13,
  397.         EngineShutDown = 13,
  398.         Super = 17,
  399.         Shields = 18,
  400.         Repel = 21,
  401.         Burst = 22,
  402.         Decoy = 23,
  403.         Thor = 24,
  404.         Multi = 25,
  405.         Brick = 26,
  406.         Rocket = 27,
  407.         Portal = 28
  408.     }
  409. }
Advertisement
Add Comment
Please, Sign In to add comment