Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using BattleCore.Events;
- using BattleCorePsyOps;
- using System.IO;
- namespace BattleZone
- {
- public class GreenControl
- {
- // My custom chat mod
- ShortChat msg = new ShortChat();
- // Use this for random numbers
- private Random ran = new Random();
- // List of all the active green indexes- a list of all active greens.
- private List<int> _ActiveGreenIndex = new List<int>();
- // All greens and green status - if we activate put the index into _ActiveGreenIndex
- private List<CustomGreen> _GreenMasterList = new List<CustomGreen>();
- // Each cycle this will tell use if we have to send another gfx command
- private bool _needUpdateLvz = false;
- // This is the gfc command string
- private string _toggleGreenz = "*objset ";
- // We stack them into this q
- private Queue<EventArgs> _CommandUpdates = new Queue<EventArgs>();
- // How many active greens we want out at a time
- private byte _ActiveGreens = 5;
- // Use this for debug
- public void Test()
- { turnRandomGreenOn(false); }
- // Add greens to the master list - this comes from cfg file
- private void RegisterGreen(ushort LvzStartIndex, ushort x, ushort y, byte radius)
- { _GreenMasterList.Add(new CustomGreen(LvzStartIndex, x, y, radius)); }
- // This is a timed event - updates all expired greens
- // and returns all toggled lvz commands. Objset
- public Queue<EventArgs> UpdateGreenGfx
- {
- get{
- // Make sure cfg was loaded
- if (_GreenMasterList.Count > 0)
- {
- // Check expired greens
- for (int i = 0; i < _ActiveGreenIndex.Count; i += 1)
- {
- int index = _ActiveGreenIndex[i];
- if (_GreenMasterList[index].GreenExpired)
- turnGreenOff(i, index);
- }
- // Fill greens to active green count
- if (_ActiveGreenIndex.Count < _ActiveGreens)
- turnRandomGreenOn(true);
- // Dump the command into chat q
- if (_needUpdateLvz)
- {
- _CommandUpdates.Enqueue(msg.pub(_toggleGreenz));
- _toggleGreenz = "*objset ";
- _needUpdateLvz = false;
- }
- // Send the chat q
- if (_CommandUpdates.Count > 0)
- return _CommandUpdates;
- }
- return null;
- }
- }
- // All required tasks to turn a single green off. onPlayerGetGreen or expired time
- private void turnGreenOff(int GIndex, int index)
- {
- _GreenMasterList[index].Active = false;
- _toggleGreenz += "-" + (_GreenMasterList[index].LvzID + GetPrizeID(_GreenMasterList[index].Type)) + ",";
- _ActiveGreenIndex.RemoveAt(GIndex);
- _needUpdateLvz = true;
- }
- private int _NextRegen = 10;
- private DateTime _RegenTimeStamp = DateTime.Now;
- // All required tasks to turn on a random green on
- private void turnRandomGreenOn( bool RanTime)
- {
- if (RanTime && (DateTime.Now - _RegenTimeStamp).TotalSeconds >= _NextRegen || !RanTime)
- {
- _RegenTimeStamp = DateTime.Now;
- _NextRegen = ran.Next(1, 30);
- turnGreenOn();
- }
- }
- // Turn on random green - random prize - random time
- private void turnGreenOn()
- {
- bool searching = true;
- while (searching)
- {
- // making sure i dont create an infinite loop if all greens have been spawned
- if (_ActiveGreenIndex.Count >= _GreenMasterList.Count) searching = false;
- // get a random number for green spawn
- int index = ran.Next(0, _GreenMasterList.Count);
- // checking if the green hasnt been already activated
- if (!_GreenMasterList[index].Active)
- {
- turnGreenOn(index);
- searching = false;
- }
- }
- }
- // Turn on green - random prize
- private void turnGreenOn(int index)
- {
- turnGreenOn(index, ran.Next(0, 11));
- }
- // Turn on specific green
- private void turnGreenOn(int index, int PrizeIndex)
- {
- _GreenMasterList[index].Active = true;
- _GreenMasterList[index].Type = PrizeLvzIDList[PrizeIndex];
- _ActiveGreenIndex.Add(index);
- _toggleGreenz += "+" + (_GreenMasterList[index].LvzID + GetPrizeID(_GreenMasterList[index].Type)) + ",";
- _GreenMasterList[index].SetTimeStamp();
- _GreenMasterList[index].SetAliveTime(ran.Next(20,100));
- _needUpdateLvz = true;
- }
- // Basically collision detection on greens and players
- public void CheckGreenRegions(PlayerPositionEvent p)
- {
- if (_ActiveGreenIndex.Count > 0)
- for (int i = 0; i < _ActiveGreenIndex.Count; i += 1)
- if (InHotSpot(p, _GreenMasterList[_ActiveGreenIndex[i]].HotSpot))
- {
- int index = _ActiveGreenIndex[i];
- turnGreenOff(i,index);
- if (_GreenMasterList[index].Type != PrizeType.Multi)
- {
- _CommandUpdates.Enqueue(msg.pm(p.PlayerName, "*prize #" + ((byte)_GreenMasterList[index].Type + 0)));
- }
- else
- _CommandUpdates.Enqueue(msg.pm(p.PlayerName, "*prize 500"));
- return;
- }
- }
- // Collision detection utility method
- private bool InHotSpot(PlayerPositionEvent Player, ushort[] HotSpot)
- {
- if (Player.MapPositionX > HotSpot[0] && Player.MapPositionX < HotSpot[2]
- && Player.MapPositionY > HotSpot[1] && Player.MapPositionY < HotSpot[3])
- return true;
- return false;
- }
- // These 2 methods help me return the index which is = to lvz ID numbers.
- // Add this to the lvz id start index and you have your lvz ID for that prize.
- private PrizeType[] PrizeLvzIDList = new PrizeType[12]
- {
- PrizeType.FullCharge, PrizeType.EngineShutDown, PrizeType.Super, PrizeType.Shields,
- PrizeType.Repel, PrizeType.Burst, PrizeType.Decoy, PrizeType.Thor,
- PrizeType.Multi, PrizeType.Brick, PrizeType.Rocket, PrizeType.Portal,
- };
- private int GetPrizeID(PrizeType prize)
- {
- for (int i = 0; i < PrizeLvzIDList.Length; i += 1)
- if (prize == PrizeLvzIDList[i]) return i;
- return 0;
- }
- // Green object that holds all the info required to spawn and toggle lvz
- private class CustomGreen
- {
- // Creates the green and hotspot needed for collision
- public CustomGreen(ushort LvzID, ushort xCoord, ushort yCoord, byte Radius)
- {
- this._LvzID = LvzID;
- this._HotSpot = new ushort[4]
- {
- (ushort)(xCoord - Radius),
- (ushort)(yCoord - Radius),
- (ushort)(xCoord + Radius),
- (ushort)(yCoord + Radius)
- };
- }
- // Lvz start id index
- // Add the prize index to this and you have ID for that green and that prize
- private ushort _LvzID;
- public ushort LvzID
- { get { return _LvzID; } }
- // if green is active or not
- private bool _Active = false;
- public bool Active
- {
- get { return _Active; }
- set { _Active = value; }
- }
- // These are used to create timestamps and keep track of green alive times
- private int _ExpireTime = 10;
- public int ExpireTime
- { get { return _ExpireTime; } }
- private DateTime _TimeStamp = DateTime.Now;
- public DateTime TimeStamp { get { return _TimeStamp; } }
- public void SetAliveTime(int ExpireTime)
- { _ExpireTime = ExpireTime; }
- public void SetTimeStamp()
- { _TimeStamp = DateTime.Now; }
- public bool GreenExpired
- {
- get
- {
- if ((DateTime.Now - _TimeStamp).TotalSeconds >= _ExpireTime)
- return true;
- return false;
- }
- }
- // What prize you want to set for a green
- private PrizeType _Type = PrizeType.FullCharge;
- public PrizeType Type
- {
- get { return _Type; }
- set { _Type = value; }
- }
- // Hotspots for the greens
- private ushort[] _HotSpot = new ushort[4];
- public ushort[] HotSpot { get { return _HotSpot; } }
- }
- // -------------------------------------- CFG get setting code //
- // ----------------------------------------------------------- //
- private Queue<EventArgs> _cfgPrintOut = new Queue<EventArgs>();
- public Queue<EventArgs> CfgPrintout
- { get { return _cfgPrintOut; } }
- public void LoadCFG(string BotName)
- {
- if (File.Exists(BotName + "/customgreens.cfg"))
- {
- try
- {
- //Opening appropriate file
- StreamReader SR = File.OpenText(BotName + "/customgreens.cfg");
- //used to read each line
- string S;
- int AmountOfPrizes = 12;
- bool MakeIni = false;
- string Lvzfilename = "";
- string[] iniSettings = new string[12];
- int[] LvzDimensions = new int[12];
- int DimIndex = 0;
- int IniIndex = 0;
- ushort[] GreenArea = new ushort[4];
- int XRowCol = 0;
- int YRowCol = 0;
- // We will store the INI file in here line by line
- // and print it out later for file creation
- Queue<string> SavedIni = new Queue<string>();
- // Extract and save all info from the cfg file
- while ((S = SR.ReadLine()) != null)
- {
- if (!S.StartsWith(";"))
- {
- string[] data = S.Trim().Split(':');
- if (S.StartsWith("createini:"))
- {
- if (data[1].ToLower().Equals("yes"))
- MakeIni = true;
- _cfgPrintOut.Enqueue(msg.arena("MakeIni set to [" + MakeIni + "]"));
- }
- else if (S.StartsWith("lvzfilename:"))
- Lvzfilename = data[1];
- else if (S.StartsWith("FileInfo:"))
- iniSettings[IniIndex++] = S;
- else if (S.StartsWith("Prize:"))
- {
- LvzDimensions[DimIndex++] = int.Parse(data[2]);
- }
- else if (S.StartsWith("spawnarea:"))
- {
- GreenArea[0] = (ushort)(int.Parse(data[1]) * 16);
- GreenArea[1] = (ushort)(int.Parse(data[2]) * 16);
- GreenArea[2] = (ushort)(int.Parse(data[3]) * 16);
- GreenArea[3] = (ushort)(int.Parse(data[4]) * 16);
- XRowCol = int.Parse(data[5]);
- YRowCol = int.Parse(data[6]);
- }
- }
- }
- // Closing file
- SR.Close();
- // Save the interval we want set each green
- int xGap = (int)Math.Floor((double)((GreenArea[2] - GreenArea[0]) / XRowCol));
- int yGap = (int)Math.Floor((double)((GreenArea[3] - GreenArea[1]) / YRowCol));
- // Start x and y coords of the green area
- int NextX = GreenArea[0];
- int NextY = GreenArea[1];
- if (MakeIni)
- {
- // ----------------------------------------------- Ini generate file
- // Filename
- SavedIni.Enqueue("Outfile=" + Lvzfilename);
- // File listing
- for (int i = 0; i < AmountOfPrizes; i += 1)
- SavedIni.Enqueue("File=" + iniSettings[i].Split(':')[1]);
- // Object image listing
- SavedIni.Enqueue("[objectimages]");
- for (int i = 0; i < AmountOfPrizes; i += 1)
- SavedIni.Enqueue("IMAGE" + i + "=" + iniSettings[i].Split(':')[1] + "," + iniSettings[i].Split(':')[2]);
- // Map Objects title
- SavedIni.Enqueue("[mapobjects]");
- }
- int IDCount = 0;
- // Loop and position all greens
- // Create the green list
- // If setting is on Create the ini file as well
- for (int i = 0; i < XRowCol; i += 1)
- {
- int xCoor = NextX;
- NextX += xGap;
- for (int j = 0; j < YRowCol; j += 1)
- {
- // reset Y once it gets to end
- if (NextY > GreenArea[3])
- NextY = GreenArea[1];
- // Increment the interval that we assigned
- int yCoor = NextY;
- NextY += yGap;
- // ---------------Create the greens in here--------------- \\
- RegisterGreen((ushort)IDCount, (ushort)xCoor, (ushort)yCoor, 32);
- for (int k = 0; k < AmountOfPrizes; k += 1)
- {
- int LvzID = IDCount++;
- if (MakeIni)
- SavedIni.Enqueue((xCoor - LvzDimensions[k] / 2) + "," +
- (yCoor - LvzDimensions[k] / 2) + ",IMAGE" + k + ",AfterShips,ServerControlled,0," + LvzID);
- }
- }
- }
- _cfgPrintOut.Enqueue(msg.arena("Greens created: [" + _GreenMasterList.Count + "]."));
- if (MakeIni)
- {
- //CustomGreenLVZ
- using (StreamWriter sw = new StreamWriter("lvz/customgreens/CustomGreenLVZ.ini"))
- {
- while (SavedIni.Count > 0)
- sw.WriteLine(SavedIni.Dequeue());
- }
- _cfgPrintOut.Enqueue(msg.arena("Ini successfuly created."));
- return;
- }
- else
- _cfgPrintOut.Enqueue(msg.arena("Create Ini: Off. Ini not created."));
- return;
- }
- catch (Exception ex) { _cfgPrintOut.Enqueue(msg.arena("Error:" + ex)); return;}
- }
- _cfgPrintOut.Enqueue(msg.arena("File not found: Ini not created."));
- }
- }
- // Prizes and their *prize #s
- public enum PrizeType
- {
- FullCharge = 13,
- EngineShutDown = 13,
- Super = 17,
- Shields = 18,
- Repel = 21,
- Burst = 22,
- Decoy = 23,
- Thor = 24,
- Multi = 25,
- Brick = 26,
- Rocket = 27,
- Portal = 28
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment