Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*************************************************
- * Automatic Air-Lock Script - by Syntak *
- *************************************************/
- //1.14
- public Program()
- {
- //ReadUserOptions();
- AirLockList = new List<AirLock>();
- airLockCount = CacheBlockLists();
- lastUpdateTime = System.DateTime.Now;
- Runtime.UpdateFrequency = UpdateFrequency.Update10;
- Echo("Hello World");
- //if (Me.CustomData.Length == 0)
- //{
- // WriteUserOptions();
- //}
- }
- //OPTIONS
- static bool LOCKDOORS = true; //LOCK DOORS TO PREVENT OPENING BEFORE DE/PRESSURISING
- static bool AUTOOPENDOORS = true; //OPEN DOORS AUTOMATICALLY AFTER DE/PRESSURISING
- static bool AUTOCLOSEDOORS = true; //CLOSE DOORS AUTOMATICALLY WHEN NOT NEAR AIRLOCK
- static float DOORCLOSEDELAY = 5000f; //miliseconds, autoclose delay for sensorless doors
- static string DISABLED_VENT = "OFF"; //behaviour of AL vents when AL disabled, OFF|DEPRESSURISE|PRESSURISE
- //SOUND
- static bool ENABLE_SOUNDS = true;
- static string EQUALISING_SOUND = "Alert 2"; //sound to play while airlock is equalising
- static string READY_SOUND = "Alert 1"; //sound to play when airlock is ready
- static float LOOP_PERIOD_EQ = 20; // Loop period for equalising sound, set to something longer that the time to cycle airlock
- static float LOOP_PERIOD_READY = 0.5f; // Loop period for ready sound, set to something short
- //PERFORMANCE
- static int CACHE_INTERVAL = 10; //seconds, time between searching for blocks. lower numbers impact performance
- static int UPDATE_INTERVAL = 50; //miliseconds, time between updating
- static double INSTRUCTION_LIMIT = 0.9; //Portion of instruction limit to use (lower for better performance)
- //BLOCK TAGS
- //static string AL_TAG_PREFIX = "AL1";
- static string AL_VENT_TAG = "AL1V"; //TAG FOR AIR LOCK VENTS
- static string AL_SEN_TAG = "AL1S"; /*TAG FOR INTRA AIR LOCK SENSOR;
- SHOULD COVER AREA INSIDE AIRLOCK*/
- static string AL_IVENT_TAG = "AL1I"; //TAG FOR INSIDE AIR VENT
- static string AL_IDOOR_TAG = "AL1I"; //TAG FOR INNER DOOR
- static string AL_ISEN_TAG = "AL1I"; /*TAG FOR INNER SENSOR;
- SHOULD COVER AREA EITHER SIDE OF INNER DOOR
- CAN'T OVERLAP WITH OUTER SENSOR AREA*/
- static string AL_ILCD_TAG = "AL1I"; //TAG FOR INNER STATUS LCD
- static string AL_ILIGHT_TAG = "AL1I"; //TAG FOR INNER STATUS LIGHT
- static string AL_ODOOR_TAG = "AL1O"; //TAG FOR OUTER DOOR
- static string AL_OSEN_TAG = "AL1O"; /*TAG FOR OUTER SENSOR;
- SHOULD COVER AREA EITHER SIDE OF OUTER DOOR
- CAN'T OVERLAP WITH INNER SENSOR AREA*/
- static string AL_OLCD_TAG = "AL1O"; //TAG FOR OUTER STATUS LCD
- static string AL_OLIGHT_TAG = "AL1O"; //TAG FOR OUTER STATUS LIGHT
- static string AL_OVENT_TAG = "AL1O"; /*TAG FOR OUTSIDE AIR VENT;
- NOT REQUIERED UNLESS BOTH SIDES OF
- THE AIR LOCK CAN BE PRESSURISED*/
- static string AL_TANK_TAG = "AL1T"; //TAG FOR OXYGEN TANK THAT FEEDS THIS AIRLOCK
- static string AL_SOUND_TAG = "AL1S"; //TAG FOR SOUND BLOCK
- static string AL_STATUS_LCD = "ALSTAT"; //TAG FOR STATUS LCDS
- //static string AL_DEBUG = "ALDEBUG";
- //COLORS
- static Color STOP_COLOR = new Color(64, 0, 0, 255); //RED
- static Color GO_COLOR = new Color(0, 64, 0, 255); //GREEN
- static Color CAUTION_COLOR = new Color(64, 64, 0, 255); //YELLOW
- static bool SMOOTH_COLOR = false;
- static Color SMOOTH_STOP_COLOR = new Color(64, 0, 0, 255); //RED
- static Color SMOOTH_GO_COLOR = new Color(0, 64, 0, 255); //GREEN
- //LCD TEXT
- static float FONT_SIZE = 1.5F;
- static string READY_TEXT = TextTools.Center(" AIRLOCK", FONT_SIZE); //GHETTO CENTER, 18 SPACES
- static string STANDBY_TEXT = TextTools.Center(" AIRLOCK", FONT_SIZE);
- static string CAUTION_TEXT = TextTools.Center(" AIRLOCK", FONT_SIZE);
- static string FULL_TANKS_TEXT = TextTools.Center("[ OXYGEN TANKS FULL ]| OPEN DOOR MANUALY", FONT_SIZE);
- static string EMPTY_TANKS_TEXT = TextTools.Center("[ OXYGEN TANKS EMPTY ]| OPEN DOOR MANUALY", FONT_SIZE);
- static string DISABLED_TEXT = TextTools.Center(" AIRLOCK DISABLED", FONT_SIZE);
- static int LCD_WIDTH = (int) (1/FONT_SIZE * 95); //SINGLE WIDTH LCD
- static string TITLE_TAG_OPEN = "[title]";
- static string TITLE_TAG_CLOSE = "[/title]";
- static float DEFALT_PRESSURE = 0;
- static float SMALL_DIFF = 0.1F;
- static float MOD_DIFF = 0.5F;
- //static float BIG_DIFF = 0.9F;
- static bool DEBUG = false; //show debug
- /*--------------Don't change anything beow this line----------------*/
- DateTime lastChacheTime;
- DateTime lastUpdateTime;
- int highestICount = 0; //instruction count
- double highestRunTime = 0;
- List<AirLock> AirLockList;
- int startChacheFrom = 0;
- int startUpdateFrom = 0;
- int airLockCount = 0;
- //bool CMD_VENT = false;
- //bool CMD_ENABLED = true;
- //bool hasArgument = false;
- int airLockIndex = -1; //zero indexed, 0 -> airlock 1
- List<IMyTerminalBlock> statusLCDs = new List<IMyTerminalBlock>();
- IMyTextPanel tpanel;
- public void Main(string argument)
- {
- if (argument != null)
- {
- //hasArgument = true;
- if (argument.ToUpper() == "DISABLE")
- {
- for (int i = 0; i < AirLockList.Count; i++)
- {
- AirLockList[i].Enabled = false;
- }
- }
- if (argument.ToUpper() == "ENABLE")
- {
- for (int i = 0; i < AirLockList.Count; i++)
- {
- AirLockList[i].Enabled = true;
- AirLockList[i].Venting = false;
- }
- }
- if (argument.ToUpper() == "VENT")
- {
- for (int i = 0; i < AirLockList.Count; i++)
- {
- AirLockList[i].Venting = true;
- }
- }
- if (argument.ToUpper() == "SCAN")
- {
- airLockCount = CacheBlockLists();
- return;
- }
- }
- else
- {
- //hasArgument = false;
- }
- if ((System.DateTime.Now - lastUpdateTime).TotalMilliseconds < UPDATE_INTERVAL)
- {
- return;
- }
- lastUpdateTime = System.DateTime.Now;
- if ((System.DateTime.Now - lastChacheTime).TotalSeconds > CACHE_INTERVAL) //search for blocks, update options, status
- {
- GridTerminalSystem.SearchBlocksOfName(AL_STATUS_LCD, statusLCDs);
- //ReadUserOptions();
- airLockCount = CacheBlockLists();
- foreach (AirLock a in AirLockList)
- {
- a.ReadUserOptions();
- }
- return;
- }
- foreach (IMyTerminalBlock t in statusLCDs)
- {
- tpanel = t as IMyTextPanel;
- tpanel.WritePublicText("Air Lock Status:" + "\n");
- tpanel.ShowPublicTextOnScreen();
- }
- for (airLockIndex = startUpdateFrom; airLockIndex < AirLockList.Count-1; airLockIndex++)
- {
- if (((double)Runtime.CurrentInstructionCount / (double)Runtime.MaxInstructionCount) > INSTRUCTION_LIMIT)
- {
- startUpdateFrom = airLockIndex;
- break;
- }
- AirLockList[airLockIndex].Update();
- //status LCD
- foreach (IMyTerminalBlock t in statusLCDs)
- {
- tpanel = t as IMyTextPanel;
- tpanel.WritePublicText("\n" + AirLockList[airLockIndex].ToString(), true);
- }
- }
- if (airLockIndex == AirLockList.Count)
- {
- startUpdateFrom = 0;
- }
- if (DEBUG)
- {
- Echo("Updating " + airLockIndex + "/" + AirLockList.Count + " airlocks");
- //Echo("Elements:" + alBlocks.Count.ToString());
- if (Runtime.CurrentInstructionCount > highestICount)
- highestICount = Runtime.CurrentInstructionCount;
- Echo("Instructions Used: " + Runtime.CurrentInstructionCount.ToString());
- Echo(((double)Runtime.CurrentInstructionCount / (double)Runtime.MaxInstructionCount * 100).ToString() + "%");
- Echo("Max: " + highestICount);
- Echo(((double)highestICount / (double)Runtime.MaxInstructionCount * 100).ToString() + "%" );
- Echo(Runtime.LastRunTimeMs + "ms");
- if (Runtime.LastRunTimeMs > highestRunTime)
- highestRunTime = Runtime.LastRunTimeMs;
- Echo("Max: " + highestRunTime);
- Echo("Status LCDS: " + statusLCDs.Count);
- }
- }
- public int FindBlocks(AirLock al)
- {
- int blocksFound = 0;
- blocksFound += SearchTag<IMyDoor>(AL_IDOOR_TAG.Replace("1", al.id.ToString()), al.innerDoors);
- blocksFound += SearchTag<IMySensorBlock>(AL_ISEN_TAG.Replace("1", al.id.ToString()), al.innerSensors);
- blocksFound += SearchTag<IMyTextPanel>(AL_ILCD_TAG.Replace("1", al.id.ToString()), al.innerLCDs);
- blocksFound += SearchTag<IMyAirVent>(AL_IVENT_TAG.Replace("1", al.id.ToString()), al.innerVents);
- blocksFound += SearchTag<IMyLightingBlock>(AL_ILIGHT_TAG.Replace("1", al.id.ToString()), al.innerLights);
- blocksFound += SearchTag<IMyAirVent>(AL_VENT_TAG.Replace("1", al.id.ToString()), al.alVents);
- blocksFound += SearchTag<IMySensorBlock>(AL_SEN_TAG.Replace("1", al.id.ToString()), al.alSensors);
- blocksFound += SearchTag<IMyDoor>(AL_ODOOR_TAG.Replace("1", al.id.ToString()), al.outerDoors);
- blocksFound += SearchTag<IMySensorBlock>(AL_OSEN_TAG.Replace("1", al.id.ToString()), al.outerSensors);
- blocksFound += SearchTag<IMyTextPanel>(AL_OLCD_TAG.Replace("1", al.id.ToString()), al.outerLCDs);
- blocksFound += SearchTag<IMyAirVent>(AL_OVENT_TAG.Replace("1", al.id.ToString()), al.outerVents);
- blocksFound += SearchTag<IMyLightingBlock>(AL_OLIGHT_TAG.Replace("1", al.id.ToString()), al.outerLights);
- blocksFound += SearchTag<IMyGasTank>(AL_TANK_TAG.Replace("1", al.id.ToString()), al.tanks);
- blocksFound += SearchTag<IMySoundBlock>(AL_SOUND_TAG.Replace("1", al.id.ToString()), al.soundblocks);
- return blocksFound;
- }
- public int SearchTag<T>(string tag, List<IMyTerminalBlock> finallist) where T : class
- {//returns number of blocks found
- finallist.Clear();
- //get tagged blocks
- List<IMyTerminalBlock> listname = new List<IMyTerminalBlock>();
- GridTerminalSystem.SearchBlocksOfName(tag, listname);
- //get all blocks of type
- List<IMyTerminalBlock> listtype = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<T>(listtype);
- //check tagged blocks are right type
- for (int i = 0; i < listname.Count; i++)
- {
- if (listname[i] is T)
- finallist.Add(listname[i]);
- }
- //get blocks tagged using custom data
- List<IMyTerminalBlock> listcustom = new List<IMyTerminalBlock>();
- for (int i = 0; i < listtype.Count; i++)
- {
- if (listtype[i].CustomData.Contains(tag) && !(listtype[i] == Me))
- {
- listcustom.Add(listtype[i]);
- }
- }
- finallist.AddRange(listcustom);
- //GROUPS
- IMyBlockGroup taggedGroup = GridTerminalSystem.GetBlockGroupWithName(tag);
- List<IMyTerminalBlock> listGroup = new List<IMyTerminalBlock>();
- if (taggedGroup != null)
- {
- taggedGroup.GetBlocks(listGroup);
- }
- //check group tagged blocks are right type
- for (int i = 0; i < listGroup.Count; i++)
- {
- if (listGroup[i] is T)
- finallist.Add(listGroup[i]);
- }
- return finallist.Count;
- }
- public float AbsDiff(float a, float b)
- {
- float result = a - b;
- if (result < 0)
- return result * -1;
- else
- return result;
- }
- public int CacheBlockLists()
- {
- //find new air locks
- int i = startChacheFrom; //airlockindex
- if(AirLockList.Count == 0)
- {
- AirLockList.Add(new AirLock(1));
- }
- while (((double)Runtime.CurrentInstructionCount / (double)Runtime.MaxInstructionCount) < INSTRUCTION_LIMIT)
- {
- if (i >= AirLockList.Count)
- {
- AirLockList.Add(new AirLock(i + 1));
- }
- if (FindBlocks(AirLockList[i]) > 0)
- {
- i++;
- }
- else //no blocks for airlock i, all airlocks found (probably)
- {
- startChacheFrom = 0;
- lastChacheTime = System.DateTime.Now;
- return i;
- }
- }
- startChacheFrom = i;
- return i;
- }
- public void WriteUserOptions()
- {
- StringBuilder sb = new StringBuilder();
- sb.AppendLine("LOCKDOORS = " + LOCKDOORS.ToString());
- sb.AppendLine("AUTOOPENDOORS = " + AUTOOPENDOORS.ToString());
- sb.AppendLine("AUTOCLOSEDOORS = " + AUTOCLOSEDOORS.ToString());
- sb.AppendLine("DOORCLOSEDELAY = " + DOORCLOSEDELAY.ToString());
- sb.AppendLine("DISABLED_VENT = " + DISABLED_VENT.ToString());
- sb.AppendLine("ENABLE_SOUNDS = " + ENABLE_SOUNDS.ToString());
- sb.AppendLine("EQUALISING_SOUND = " + EQUALISING_SOUND.ToString());
- sb.AppendLine("READY_SOUND = " + READY_SOUND.ToString());
- sb.AppendLine("LOOP_PERIOD_EQ = " + LOOP_PERIOD_EQ.ToString());
- sb.AppendLine("LOOP_PERIOD_READY = " + LOOP_PERIOD_READY.ToString());
- sb.AppendLine("STOP_COLOR = " + STOP_COLOR);
- sb.AppendLine("GO_COLOR = " + GO_COLOR);
- sb.AppendLine("CAUTION_COLOR = " + CAUTION_COLOR);
- sb.AppendLine("SMOOTH_COLOR = " + SMOOTH_COLOR);
- sb.AppendLine("SMOOTH_STOP_COLOR = " + SMOOTH_STOP_COLOR);
- sb.AppendLine("SMOOTH_GO_COLOR = " + SMOOTH_GO_COLOR);
- sb.AppendLine("FONT_SIZE = " + FONT_SIZE);
- sb.AppendLine("READY_TEXT = " + READY_TEXT);
- sb.AppendLine("STANDBY_TEXT = " + STANDBY_TEXT);
- sb.AppendLine("CAUTION_TEXT = " + CAUTION_TEXT);
- sb.AppendLine("FULL_TANKS_TEXT = " + FULL_TANKS_TEXT);
- sb.AppendLine("EMPTY_TANKS_TEXT = " + EMPTY_TANKS_TEXT);
- sb.AppendLine("DISABLED_TEXT = " + DISABLED_TEXT);
- sb.AppendLine("CACHE_INTERVAL = " + CACHE_INTERVAL);
- sb.AppendLine("UPDATE_INTERVAL = " + UPDATE_INTERVAL);
- sb.AppendLine("INSTRUCTION_LIMIT = " + INSTRUCTION_LIMIT);
- Me.CustomData = sb.ToString();
- }
- public void ReadUserOptions()
- {
- //split on " = " (note spaces)
- string[] delim1 = {"\n"};
- string[] delim2 = { " = " };
- string[] lines = Me.CustomData.Split(delim1, StringSplitOptions.RemoveEmptyEntries);
- string var;
- string val;
- string[] varval;
- foreach(string s in lines)
- {
- varval = s.Split(delim2, 2, StringSplitOptions.RemoveEmptyEntries);
- var = varval[0];
- val = varval[1];
- Echo("PAIR:" + var + ":" + val);
- switch(var)
- {
- case "LOCKDOORS":
- SetVar(LOCKDOORS, val);
- break;
- case "AUTOOPENDOORS":
- SetVar(AUTOOPENDOORS, val);
- break;
- case "AUTOCLOSEDOORS":
- SetVar(AUTOCLOSEDOORS, val);
- break;
- case "DOORCLOSEDELAY":
- SetVar(DOORCLOSEDELAY, val);
- break;
- case "DISABLED_VENT":
- SetVar(DISABLED_VENT, val);
- break;
- case "ENABLE_SOUNDS":
- SetVar(ENABLE_SOUNDS, val);
- break;
- case "EQUALISING_SOUND":
- SetVar(EQUALISING_SOUND, val);
- break;
- case "READY_SOUND":
- SetVar(READY_SOUND, val);
- break;
- case "LOOP_PERIOD_EQ":
- SetVar(LOOP_PERIOD_EQ, val);
- break;
- case "LOOP_PERIOD_READY":
- SetVar(LOOP_PERIOD_READY, val);
- break;
- case "STOP_COLOR":
- SetVar(STOP_COLOR, val);
- break;
- case "GO_COLOR":
- SetVar(GO_COLOR, val);
- break;
- case "CAUTION_COLOR":
- SetVar(CAUTION_COLOR, val);
- break;
- case "SMOOTH_COLOR":
- SetVar(SMOOTH_COLOR, val);
- break;
- case "SMOOTH_STOP_COLOR":
- SetVar(SMOOTH_STOP_COLOR, val);
- break;
- case "SMOOTH_GO_COLOR":
- SetVar(SMOOTH_GO_COLOR, val);
- break;
- case "FONT_SIZE":
- SetVar(FONT_SIZE, val);
- break;
- case "READY_TEXT":
- SetVar(READY_TEXT, val);
- break;
- case "STANDBY_TEXT":
- SetVar(STANDBY_TEXT, val);
- break;
- case "CAUTION_TEXT":
- SetVar(CAUTION_TEXT, val);
- break;
- case "FULL_TANKS_TEXT":
- SetVar(FULL_TANKS_TEXT, val);
- break;
- case "EMPTY_TANKS_TEXT":
- SetVar(EMPTY_TANKS_TEXT, val);
- break;
- case "DISABLED_TEXT":
- SetVar(DISABLED_TEXT, val);
- break;
- case "CACHE_INTERVAL":
- SetVar(CACHE_INTERVAL, val);
- break;
- case "UPDATE_INTERVAL":
- SetVar(UPDATE_INTERVAL, val);
- break;
- case "INSTRUCTION_LIMIT":
- SetVar(INSTRUCTION_LIMIT, val);
- break;
- default:
- Echo("Check Options List!");
- break;
- }
- }
- }
- public bool SetVar(bool variable, string value)
- {
- //return true iff susessful
- if (value.ToUpper().Contains("TRUE"))
- {
- variable = true;
- return true;
- }
- if (value.ToUpper().Contains("FALSE"))
- {
- variable = false;
- return true;
- }
- Echo("Not Set");
- return false;
- }
- public bool SetVar(string variable, string value)
- {
- //return true iff susessful
- variable = value;
- return true;
- }
- public bool SetVar(int variable, string value)
- {
- //return true iff susessful
- try
- {
- variable = Convert.ToInt32(value);
- }
- catch (FormatException e)
- {
- Echo("Input string is not a sequence of digits.");
- Echo("Not Set");
- return false;
- }
- return true;
- }
- public bool SetVar(float variable, string value)
- {
- //return true iff susessful
- try
- {
- variable = Convert.ToSingle(value);
- }
- catch (FormatException e)
- {
- Echo("Input string is not a sequence of digits.");
- Echo("Not Set");
- return false;
- }
- return true;
- }
- public bool SetVar(double variable, string value)
- {
- //return true iff susessful
- try
- {
- variable = Convert.ToDouble(value);
- }
- catch (FormatException e)
- {
- Echo("Input string is not a sequence of digits.");
- Echo("Not Set");
- return false;
- }
- return true;
- }
- public bool SetVar(Color variable, string value)
- {
- //return true iff susessful
- //format: {R:64 G:0 B:0 A:255}
- char[] b = { '{', '}'};
- value = value.Trim(b); //trim brackets
- string[] rgba = value.Split(' ');
- string[] pair;
- foreach (string s in rgba)
- {
- pair = s.Split(':');
- try
- {
- switch (pair[0])
- {
- case "R":
- variable.R = Convert.ToByte(pair[1]);
- break;
- case "G":
- variable.G = Convert.ToByte(pair[1]);
- break;
- case "B":
- variable.B = Convert.ToByte(pair[1]);
- break;
- case "A":
- variable.A = Convert.ToByte(pair[1]);
- break;
- default:
- Echo("Color format incorrect");
- Echo("Not Set");
- return false;
- }
- }
- catch (FormatException e)
- {
- Echo("Input string is not a sequence of digits.");
- Echo("Not Set");
- return false;
- }
- }
- return true;
- }
- public class AirLock
- {
- //OPTIONS
- bool lockDoors { get; set; }
- bool autoOpenDoors { get; set; }
- bool autoCloseDoors { get; set; }
- float doorCloseDelay { get; set; }
- string disabled_vent { get; set; }
- bool enable_sounds { get; set; }
- string equalising_sound { get; set; }
- string ready_sound { get; set; }
- float loop_period_eq { get; set; }
- float loop_period_ready { get; set; }
- //COLORS
- Color stop_color { get; set; }
- Color go_color { get; set; }
- Color caution_color { get; set; }
- bool smooth_color { get; set; }
- Color smooth_stop_color { get; set; }
- Color smooth_go_color { get; set; }
- //LCD TEXT
- private float _font_size;
- float font_size
- {
- get { return _font_size; }
- set
- {
- _font_size = value;
- lcd_width = (int)(1 / value * 95); //SINGLE WIDTH LCD
- }
- }
- string ready_text { get; set; }
- string standby_text { get; set; }
- string caution_text { get; set; }
- string full_tanks_text { get; set; }
- string empty_tanks_text { get; set; }
- string disabled_text { get; set; }
- int lcd_width { get; set; }
- //Al specific flags
- int lastActiveZone { get; set; }
- bool innerDoorWasOpen { get; set; }
- bool outerDoorWasOpen { get; set; }
- double timeSinceDoorOpened { get; set; }
- float timeSinceDoorOpenedInner { get; set; }
- float timeSinceDoorOpenedOuter { get; set; }
- DateTime doorTimeInner { get; set; }
- DateTime doorTimeOuter { get; set; }
- bool dontReopenInner { get; set; }
- bool dontReopenOuter { get; set; }
- bool isEqualised { get; set; }
- bool eqSoundPlayed { get; set; }
- bool readySoundPlayed { get; set; }
- private bool isventing;
- private bool isenabled;
- public int id { get; set; }
- public List<IMyTerminalBlock> innerDoors;
- public List<IMyTerminalBlock> outerDoors;
- public List<IMyTerminalBlock> innerVents;
- public List<IMyTerminalBlock> outerVents;
- public List<IMyTerminalBlock> innerSensors;
- public List<IMyTerminalBlock> outerSensors;
- public List<IMyTerminalBlock> innerLCDs;
- public List<IMyTerminalBlock> outerLCDs;
- public List<IMyTerminalBlock> innerLights;
- public List<IMyTerminalBlock> outerLights;
- public List<IMyTerminalBlock> alVents;
- public List<IMyTerminalBlock> alSensors;
- public List<IMyTerminalBlock> tanks;
- public List<IMyTerminalBlock> soundblocks;
- public bool Enabled
- {
- get { return isenabled; }
- set
- {
- isenabled = value;
- if (isenabled == true)
- TurnOn(alVents);
- if (isenabled == false)
- {
- TurnOnDoors(innerDoors);
- TurnOnDoors(outerDoors);
- if (disabled_vent == "OFF")
- TurnOff(alVents);
- if (disabled_vent == "DEPRESSURISE")
- DePressurise(alVents);
- if (disabled_vent == "PRESSURISE")
- Pressurise(alVents);
- }
- }
- }
- public bool Venting
- {
- get { return isventing; }
- set
- {
- isventing = value;
- if (isventing == true)
- {
- OpenDoors(innerDoors);
- OpenDoors(outerDoors);
- }
- }
- }
- public string centeredName { get; set; }
- private string name;
- public string Name
- {
- get { return name; }
- set
- {
- name = value;
- centeredName = TextTools.Center(name, font_size);
- ready_text = centeredName;
- standby_text = centeredName;
- caution_text = centeredName;
- }
- }
- public AirLock(int number) //constructor, make sure all variables are initilised
- {
- id = number;
- //Name = "AIRLOCK " + number.ToString();
- //Set Defalts
- lockDoors = LOCKDOORS;
- autoOpenDoors = AUTOOPENDOORS;
- autoCloseDoors = AUTOCLOSEDOORS;
- doorCloseDelay = DOORCLOSEDELAY;
- disabled_vent = DISABLED_VENT;
- enable_sounds = ENABLE_SOUNDS;
- equalising_sound = EQUALISING_SOUND;
- ready_sound = READY_SOUND;
- loop_period_eq = LOOP_PERIOD_EQ;
- loop_period_ready = LOOP_PERIOD_READY;
- stop_color = STOP_COLOR;
- go_color = GO_COLOR;
- caution_color = CAUTION_COLOR;
- smooth_color = SMOOTH_COLOR;
- smooth_stop_color = SMOOTH_STOP_COLOR;
- smooth_go_color = SMOOTH_GO_COLOR;
- font_size = FONT_SIZE;
- ready_text = centeredName;
- standby_text = centeredName;
- caution_text = centeredName;
- full_tanks_text = FULL_TANKS_TEXT;
- empty_tanks_text = EMPTY_TANKS_TEXT;
- disabled_text = DISABLED_TEXT;
- lcd_width = (int)(1 / font_size * 95); //SINGLE WIDTH LCD
- lastActiveZone = 0;
- innerDoorWasOpen = true;
- outerDoorWasOpen = true;
- timeSinceDoorOpened = 0;
- timeSinceDoorOpenedInner = 0;
- timeSinceDoorOpenedOuter = 0;
- doorTimeInner = System.DateTime.Now;
- doorTimeOuter = System.DateTime.Now;
- dontReopenInner = false;
- dontReopenOuter = false;
- isEqualised = true;
- eqSoundPlayed = true;
- readySoundPlayed = true;
- isventing = false;
- isenabled = true;
- Name = "AIRLOCK " + number.ToString();
- //init block lists
- innerDoors = new List<IMyTerminalBlock>();
- outerDoors = new List<IMyTerminalBlock>();
- innerVents = new List<IMyTerminalBlock>();
- outerVents = new List<IMyTerminalBlock>();
- innerSensors = new List<IMyTerminalBlock>();
- outerSensors = new List<IMyTerminalBlock>();
- innerLCDs = new List<IMyTerminalBlock>();
- outerLCDs = new List<IMyTerminalBlock>();
- innerLights = new List<IMyTerminalBlock>();
- outerLights = new List<IMyTerminalBlock>();
- alVents = new List<IMyTerminalBlock>();
- alSensors = new List<IMyTerminalBlock>();
- tanks = new List<IMyTerminalBlock>();
- soundblocks = new List<IMyTerminalBlock>();
- }
- public void Update()
- {
- SetLCDs();
- SetLights();
- if (ENABLE_SOUNDS)
- SetSoundBlocks();
- if (Venting || !Enabled) //disabled or venting,
- {
- if (!Enabled) //don't mess with doors
- {
- return;
- }
- OpenDoors(innerDoors); // venting, force open
- OpenDoors(outerDoors);
- return;
- }
- if ((SensorActive(outerSensors) && SensorActive(innerSensors))) //overlapping sensor zones,
- {
- CloseDoors(innerDoors);
- CloseDoors(outerDoors);
- if (lastActiveZone == 0) //out->in
- {
- if (Ilevel > 0.5)
- {
- Pressurise(alVents);
- }
- else
- {
- DePressurise(alVents);
- }
- }
- if (lastActiveZone == 1) //in->out
- {
- if (Olevel > 0.5)
- {
- Pressurise(alVents);
- }
- else
- {
- DePressurise(alVents);
- }
- }
- return;
- }
- if (SensorActive(innerSensors))
- {
- lastActiveZone = 1;
- CloseDoors(outerDoors);
- if (Ilevel > 0.5)
- {
- Pressurise(alVents);
- if (TanksAreEmpty())
- {
- TurnOn(innerDoors);
- return;
- }
- }
- else
- {
- DePressurise(alVents);
- if (TanksAreFull())
- {
- TurnOn(innerDoors);
- return;
- }
- }
- if (AbsDiff(Ilevel, ALlevel) < 0.1 && DoorsClosed(outerDoors))
- OpenDoors(innerDoors);
- return;
- }
- if (SensorActive(outerSensors))
- {
- lastActiveZone = 0;
- CloseDoors(innerDoors);
- if (Olevel > 0.5)
- {
- Pressurise(alVents);
- if (TanksAreEmpty())
- {
- TurnOn(outerDoors);
- return;
- }
- }
- else
- {
- DePressurise(alVents);
- if (TanksAreFull())
- {
- TurnOn(outerDoors);
- return;
- }
- }
- if (AbsDiff(Olevel, ALlevel) < 0.1 && DoorsClosed(innerDoors))
- {
- OpenDoors(outerDoors);
- }
- return;
- }
- if (SensorActive(alSensors))
- {
- if (innerSensors.Count != 0 && outerSensors.Count != 0) //has innner AND outer sensor
- {
- CloseDoors(innerDoors);
- CloseDoors(outerDoors);
- if (lastActiveZone == 0) //out->in
- {
- if (Ilevel > 0.5)
- {
- Pressurise(alVents);
- }
- else
- {
- DePressurise(alVents);
- }
- }
- if (lastActiveZone == 1) //in->out
- {
- if (Olevel > 0.5)
- {
- Pressurise(alVents);
- }
- else
- {
- DePressurise(alVents);
- }
- }
- return;
- }
- else //intra sensor only, no door sensors
- {
- //do nothing
- }
- }
- //sensorless doors
- bool sensorlessOuter = false;
- bool sensorlessInner = false;
- if (innerSensors.Count == 0) //no inner door sensors
- {
- sensorlessInner = true;
- if ((PressureDiff(innerVents, alVents) < SMALL_DIFF) && Samesies(innerVents, alVents) && DoorsClosed(outerDoors))
- {
- if (!dontReopenInner)
- OpenDoors(innerDoors);
- }
- else if (PressureDiff(outerVents, innerVents) < SMALL_DIFF && Samesies(outerVents, innerVents)) //pressure same inside and out
- {
- TurnOnDoors(innerDoors); //unlock doors, don't autoopen in-case al opens into hangar or similar
- }
- else
- {
- if (!TanksAreEmpty() && !TanksAreFull())
- {
- CloseDoors(innerDoors);
- dontReopenInner = false;
- }
- }
- }
- if (outerSensors.Count == 0) //no outer door sensors
- {
- sensorlessOuter = true;
- if ((PressureDiff(outerVents, alVents) < SMALL_DIFF) && Samesies(outerVents, alVents) && DoorsClosed(innerDoors))
- {
- if (!dontReopenOuter)
- OpenDoors(outerDoors);
- }
- else if (PressureDiff(outerVents, innerVents) < SMALL_DIFF && Samesies(outerVents, innerVents)) //pressure same inside and out
- {
- TurnOnDoors(outerDoors); //unlock doors, don't autoopen in-case al opens into hangar or similar
- }
- else
- {
- if (!TanksAreEmpty() && !TanksAreFull())
- {
- CloseDoors(outerDoors);
- dontReopenOuter = false;
- }
- }
- }
- if (CloseOnDelay(outerDoors, doorTimeOuter))
- {
- dontReopenOuter = true;
- }
- if (CloseOnDelay(innerDoors, doorTimeInner))
- {
- dontReopenInner = true;
- }
- if (AUTOCLOSEDOORS) //not near air-lock,
- {
- if (!sensorlessInner)
- CloseDoors(innerDoors);
- if (!sensorlessOuter)
- CloseDoors(outerDoors);
- else
- {
- if (DoorsClosed(innerDoors))
- {
- ResetDoorTimerInner();
- }
- if (DoorsClosed(outerDoors))
- {
- ResetDoorTimerOuter();
- }
- }
- }
- if (TanksAreEmpty() || TanksAreFull())
- {
- TurnOn(innerDoors);
- TurnOn(outerDoors);
- }
- }
- public float Ilevel
- {
- get { return Pressure(innerVents); }
- }
- public float Olevel
- {
- get { return Pressure(outerVents); }
- }
- public float ALlevel
- {
- get { return Pressure(alVents); }
- }
- public bool IsEqualised
- {
- get
- {
- if (AbsDiff(ALlevel, Ilevel) > SMALL_DIFF && AbsDiff(ALlevel, Olevel) > SMALL_DIFF)
- return false;
- else
- return true;
- }
- }
- public void SetLCDs()
- {
- Color screenColor;
- string screenText;
- //Inner
- if (innerLCDs.Count > 0)
- {
- float pressureDiff = AbsDiff(ALlevel, Ilevel);
- if (pressureDiff < SMALL_DIFF)
- {
- screenColor = GO_COLOR;
- screenText = ready_text;
- }
- else if (pressureDiff > MOD_DIFF)
- {
- screenColor = STOP_COLOR;
- screenText = caution_text;
- }
- else
- {
- screenColor = CAUTION_COLOR;
- screenText = standby_text;
- }
- if (smooth_color)
- {
- screenColor = SmoothColor(pressureDiff);
- }
- WriteToScreens(innerLCDs, screenColor, screenText);
- }
- //Outer
- if (outerLCDs.Count > 0)
- {
- float pressureDiff = AbsDiff(ALlevel, Olevel);
- if (pressureDiff < SMALL_DIFF)
- {
- screenColor = GO_COLOR;
- screenText = ready_text;
- }
- else if (pressureDiff > MOD_DIFF)
- {
- screenColor = STOP_COLOR;
- screenText = caution_text;
- }
- else
- {
- screenColor = CAUTION_COLOR;
- screenText = standby_text;
- }
- if (smooth_color)
- {
- screenColor = SmoothColor(pressureDiff);
- }
- WriteToScreens(outerLCDs, screenColor, screenText);
- }
- }
- private void WriteToScreens(List<IMyTerminalBlock> panels, Color screenColor, string screenText, bool showNormalText = true)
- {
- if (TanksAreEmpty())
- {
- //screenColor = CAUTION_COLOR;
- screenText = empty_tanks_text;
- showNormalText = false;
- }
- if (TanksAreFull())
- {
- //screenColor = CAUTION_COLOR;
- screenText = full_tanks_text;
- showNormalText = false;
- }
- if (!Enabled) //AIR-LOCKS DISABLED
- {
- //screenColor = CAUTION_COLOR;
- screenText = disabled_text;
- showNormalText = false;
- }
- IMyTextPanel tpanel;
- string customScreenText;
- int start, end;
- for (int i = 0; i < panels.Count; i++)
- {
- tpanel = panels[i] as IMyTextPanel;
- tpanel.SetValue("BackgroundColor", screenColor);
- tpanel.SetValue("FontSize", font_size);
- if (showNormalText && tpanel.CustomData.Contains(TITLE_TAG_OPEN)) //shownormaltext = not an error msg
- {
- start = tpanel.CustomData.IndexOf(TITLE_TAG_OPEN) + TITLE_TAG_OPEN.Length;
- if (tpanel.CustomData.Contains(TITLE_TAG_CLOSE))
- {
- end = tpanel.CustomData.IndexOf(TITLE_TAG_CLOSE, start);
- customScreenText = TextTools.Center(tpanel.CustomData.Substring(start, end - start), font_size);
- tpanel.WritePublicText(BuildDisplayText(customScreenText, ALlevel));
- }
- }
- else
- tpanel.WritePublicText(BuildDisplayText(screenText, ALlevel));
- tpanel.ShowPublicTextOnScreen();
- }
- }
- public void SetLights()
- {
- Color lightColor;
- IMyLightingBlock light;
- //Inner
- if (innerLights.Count > 0)
- {
- float pressureDiff = AbsDiff(ALlevel, Ilevel);
- if (pressureDiff < SMALL_DIFF)
- {
- lightColor = go_color;
- }
- else if (pressureDiff > MOD_DIFF)
- {
- lightColor = stop_color;
- }
- else
- {
- lightColor = caution_color;
- }
- if (SMOOTH_COLOR)
- {
- lightColor = SmoothColor(pressureDiff);
- }
- for (int i = 0; i < innerLights.Count; i++)
- {
- light = innerLights[i] as IMyLightingBlock;
- light.SetValue("Color", lightColor);
- }
- }
- //outer
- if (outerLights.Count > 0)
- {
- float pressureDiff = AbsDiff(ALlevel, Olevel);
- if (pressureDiff < SMALL_DIFF)
- {
- lightColor = go_color;
- }
- else if (pressureDiff > MOD_DIFF)
- {
- lightColor = stop_color;
- }
- else
- {
- lightColor = caution_color;
- }
- if (SMOOTH_COLOR)
- {
- lightColor = SmoothColor(pressureDiff);
- }
- for (int i = 0; i < outerLights.Count; i++)
- {
- light = outerLights[i] as IMyLightingBlock;
- light.SetValue("Color", lightColor);
- }
- }
- }
- public void SetSoundBlocks()
- {
- if (soundblocks.Count == 0 || !enable_sounds)
- return;
- if (IsEqualised)
- {
- if (!readySoundPlayed)
- {
- PlaySound(ready_sound, loop_period_ready);
- readySoundPlayed = true;
- }
- eqSoundPlayed = false; //reset eq flag
- }
- else if (!IsEqualised)
- {
- if (!eqSoundPlayed)
- {
- PlaySound(equalising_sound, loop_period_eq);
- eqSoundPlayed = true;
- }
- readySoundPlayed = false; //reset ready flag
- }
- }
- private void PlaySound(string sound, float period)
- {
- IMySoundBlock sb;
- for (int i = 0; i < soundblocks.Count; i++)
- {
- sb = soundblocks[i] as IMySoundBlock;
- sb.SelectedSound = sound;
- sb.LoopPeriod = period;
- sb.Play();
- }
- }
- private void StopSound(string sound, float period)
- {
- IMySoundBlock sb;
- for (int i = 0; i < soundblocks.Count; i++)
- {
- sb = soundblocks[i] as IMySoundBlock;
- sb.Stop();
- }
- }
- private float AbsDiff(float a, float b)
- {
- float result = a - b;
- if (result < 0)
- return result * -1;
- else
- return result;
- }
- private float Pressure(List<IMyTerminalBlock> vents)
- {
- if (vents.Count == 0)
- return DEFALT_PRESSURE;
- else
- {
- IMyAirVent vent = vents[0] as IMyAirVent;
- return vent.GetOxygenLevel();
- }
- }
- public float ALPressure()
- {
- if (alVents.Count == 0)
- return DEFALT_PRESSURE;
- else
- {
- IMyAirVent vent = alVents[0] as IMyAirVent;
- return vent.GetOxygenLevel();
- }
- }
- private bool TanksAreFull()
- {
- if (tanks.Count == 0)
- return false; //no tagged tanks, assume all ok
- IMyGasTank tank;
- for (int i = 0; i < tanks.Count; i++)
- {
- tank = tanks[i] as IMyGasTank;
- if (tank.FilledRatio < 1)
- return false;
- }
- return true;
- }
- private bool TanksAreEmpty()
- {
- if (tanks.Count == 0)
- return false; //no tagged tanks, assume all ok
- IMyGasTank tank;
- for (int i = 0; i < tanks.Count; i++)
- {
- tank = tanks[i] as IMyGasTank;
- if (tank.FilledRatio > 0)
- return false;
- }
- return true;
- }
- private bool SensorActive(List<IMyTerminalBlock> sensors)
- {
- if (sensors.Count == 0)
- return false;
- IMySensorBlock sen;
- for (int i = 0; i < sensors.Count; i++)
- {
- sen = sensors[i] as IMySensorBlock;
- if (sen.IsActive)
- return true;
- }
- return false;
- }
- private void CloseDoors(List<IMyTerminalBlock> doors)
- {
- if (doors.Count == 0)
- return;
- IMyDoor door;
- for (int i = 0; i < doors.Count; i++)
- {
- door = doors[i] as IMyDoor;
- door.ApplyAction("OnOff_On");
- door.ApplyAction("Open_Off");
- if (LOCKDOORS && door.OpenRatio == 0)
- door.ApplyAction("OnOff_Off");
- }
- }
- private void CloseDoorsUnlocked(List<IMyTerminalBlock> doors)
- {
- if (doors.Count == 0)
- return;
- IMyDoor door;
- for (int i = 0; i < doors.Count; i++)
- {
- door = doors[i] as IMyDoor;
- door.ApplyAction("OnOff_On");
- door.ApplyAction("Open_Off");
- }
- }
- public bool CloseOnDelay(List<IMyTerminalBlock> doors, DateTime dTime) //superfluious variable pass
- {
- /*closes doors if they have been open for DOORCLOSEDELAY, doors remain ON, returns true if door was closed*/
- /*takes a list of doors to close and the time they where opened*/
- if (!autoCloseDoors)
- return false;
- timeSinceDoorOpened = (System.DateTime.Now - dTime).TotalMilliseconds;
- if (timeSinceDoorOpened > doorCloseDelay)
- {
- CloseDoors(doors);
- TurnOnDoors(doors); //leave on to allow to be re-opened manualy
- return true;
- }
- else
- return false;
- }
- public void ResetDoorTimer(DateTime time)
- {
- time = System.DateTime.Now;
- }
- public void ResetDoorTimerInner()
- {
- doorTimeInner = System.DateTime.Now;
- }
- public void ResetDoorTimerOuter()
- {
- doorTimeOuter = System.DateTime.Now;
- }
- public bool DoorsClosed(List<IMyTerminalBlock> doors)
- {
- //returns false if any door is open
- if (doors.Count == 0)
- return true;
- IMyDoor d;
- for (int i = 0; i < doors.Count; i++)
- {
- d = doors[i] as IMyDoor;
- if (d.OpenRatio != 0)
- return false;
- }
- return true;
- }
- public void OpenDoors(List<IMyTerminalBlock> doors)
- {
- if (doors.Count == 0)
- return;
- IMyDoor door;
- for (int i = 0; i < doors.Count; i++)
- {
- door = doors[i] as IMyDoor;
- if (door.OpenRatio < 0.9)
- {
- if (door.Enabled)
- {
- if (AUTOOPENDOORS)
- door.ApplyAction("Open_On");
- }
- else
- door.ApplyAction("OnOff_On");
- }
- }
- }
- public void TurnOnDoors(List<IMyTerminalBlock> doors)
- {
- if (doors.Count == 0)
- return;
- IMyDoor door;
- for (int i = 0; i < doors.Count; i++)
- {
- door = doors[i] as IMyDoor;
- door.ApplyAction("OnOff_On");
- }
- }
- public void Pressurise(List<IMyTerminalBlock> vents)
- {//should always be al vents
- if (vents.Count == 0)
- return;
- IMyAirVent vent;
- for (int i = 0; i < vents.Count; i++)
- {
- vent = vents[i] as IMyAirVent;
- vent.ApplyAction("Depressurize_Off");
- }
- }
- public void DePressurise(List<IMyTerminalBlock> vents)
- {//should always be al vents
- if (vents.Count == 0)
- return;
- IMyAirVent vent;
- for (int i = 0; i < vents.Count; i++)
- {
- vent = vents[i] as IMyAirVent;
- vent.ApplyAction("Depressurize_On");
- }
- }
- public bool Samesies(List<IMyTerminalBlock> ventsO, List<IMyTerminalBlock> ventsAL)
- {
- if (ventsAL.Count == 0)
- {
- return true; // no air lock vent, still need to allow doors to open
- }
- if (ventsO.Count == 0)
- {
- return (ventsAL[0] as IMyAirVent).Depressurize; //no vent outer, treat as depressurising
- }
- IMyAirVent ventO = ventsO[0] as IMyAirVent;
- IMyAirVent ventAL = ventsAL[0] as IMyAirVent;
- if (ventO.Depressurize == ventAL.Depressurize)
- return true;
- return false;
- }
- public void TurnOff(List<IMyTerminalBlock> blocks)
- {
- if (blocks.Count == 0)
- return;
- for (int i = 0; i < blocks.Count; i++)
- {
- blocks[i].ApplyAction("OnOff_Off");
- }
- }
- public void TurnOn(List<IMyTerminalBlock> blocks)
- {
- if (blocks.Count == 0)
- return;
- for (int i = 0; i < blocks.Count; i++)
- {
- if (!blocks[i].IsWorking)
- blocks[i].ApplyAction("OnOff_On");
- }
- }
- public float PressureDiff(List<IMyTerminalBlock> ventsA, List<IMyTerminalBlock> ventsB)
- {
- float result = Pressure(ventsA) - Pressure(ventsB);
- if (result < 0)
- return result * -1;
- else
- return result;
- }
- public string ProgressBar(float progress)
- {
- string text = "";
- for (int i = 0; i < progress * lcd_width; i++)
- {
- text += '|';
- }
- return text;
- }
- public string BuildDisplayText(string text, float level)
- {
- return text + '\n' + ProgressBar(level);
- }
- public string BuildDisplayTextWide(string text, float level)
- {
- return text + '\n' + ProgressBar(level * 2);
- }
- public Color SmoothColor(float diff)
- {
- Color c = caution_color;
- c.R = (byte)(smooth_stop_color.R * diff);
- c.G = (byte)(smooth_stop_color.G * diff);
- c.B = (byte)(smooth_stop_color.B * diff);
- c.R += (byte)(smooth_go_color.R - smooth_go_color.R * diff);
- c.G += (byte)(smooth_go_color.G - smooth_go_color.G * diff);
- c.B += (byte)(smooth_go_color.B - smooth_go_color.B * diff);
- return c;
- }
- public override string ToString()
- {
- StringBuilder sb = new StringBuilder();
- sb.Append(Name + " : ");
- sb.Append("Oxygen Level: " + (int)(100 * Pressure(alVents)) + "% ");
- if (!Enabled)
- sb.Append("[DISABLED] ");
- if (isventing)
- sb.Append("[VENTING] ");
- if (TanksAreFull())
- sb.Append("[OXYGEN TANKS FULL] ");
- if (TanksAreEmpty())
- sb.Append("[OXYGEN TANKS EMPTY] ");
- if (!DoorsClosed(innerDoors) || !DoorsClosed(outerDoors))
- sb.Append("[Door Open] ");
- return sb.ToString();
- }
- public void ReadUserOptions()
- {
- string[] lines;
- string[] part;
- char[] delim = { '=' };
- float tempfloat;
- foreach (IMyTerminalBlock b in alVents)
- {
- lines = b.CustomData.Split('\n');
- foreach (string l in lines)
- {
- part = l.Split(delim, 2, StringSplitOptions.RemoveEmptyEntries);
- if (part.Length != 2)
- continue;
- if (part[0].Trim().ToUpper() == "FONT_SIZE")
- {
- if (float.TryParse(part[1].Trim(), out tempfloat))
- {
- font_size = tempfloat;
- }
- }
- if (part[0].Trim().ToUpper() == "NAME")
- {
- Name = part[1].Trim();
- }
- if (part[0].Trim().ToUpper() == "DOORCLOSEDELAY")
- {
- if(float.TryParse(part[1].Trim(), out tempfloat))
- doorCloseDelay = tempfloat;
- }
- }
- }
- }
- }
- public void Save()
- {
- //WriteUserOptions();
- }
- public static class TextTools
- {
- //static float fontSize = 1.0f;
- static public int singlePanelWidth = 660;
- static Dictionary<char, float> charWidth = new Dictionary<char, float>();
- const char PADDING_CHAR = ' ';
- static TextTools()
- {
- AddCharsToDictionary("\n", 0);
- AddCharsToDictionary("'|¦ˉ‘’‚", 6);
- AddCharsToDictionary("ј", 7);
- AddCharsToDictionary(" ", 8);
- AddCharsToDictionary("!I`ijl ¡¨¯´¸ÌÍÎÏìíîïĨĩĪīĮįİıĵĺļľłˆˇ˘˙˚˛˜˝ІЇії‹›∙", 8);
- AddCharsToDictionary("();,.1:;[]ft{}·ţťŧț", 9);
- AddCharsToDictionary("\"; -rªºŀŕŗř", 10);
- AddCharsToDictionary("*²³¹", 11);
- AddCharsToDictionary("\\°“”„", 12);
- AddCharsToDictionary("ґ", 13);
- AddCharsToDictionary("/ijтэє", 14);
- AddCharsToDictionary("L_vx«»ĹĻĽĿŁГгзлхчҐ–•", 15);
- AddCharsToDictionary("7?Jcz¢¿çćĉċčĴźżžЃЈЧавийнопсъьѓѕќ", 16);
- AddCharsToDictionary("3FKTabdeghknopqsuy£µÝàáâãäåèéêëðñòóôõöøùúûüýþÿāăąďđēĕėęěĝğġģĥħĶķńņňʼnōŏőśŝşšŢŤŦũūŭůűųŶŷŸșȚЎЗКЛбдекруцяёђћўџ", 17);
- AddCharsToDictionary("+<=>;E^~¬±¶ÈÉÊË×÷ĒĔĖĘĚЄЏЕНЭ−", 18);
- AddCharsToDictionary("#0245689CXZ¤¥ÇßĆĈĊČŹŻŽƒЁЌАБВДИЙПРСТУХЬ€", 19);
- AddCharsToDictionary("$&GHPUVY§ÙÚÛÜÞĀĜĞĠĢĤĦŨŪŬŮŰŲОФЦЪЯжы†‡", 20);
- AddCharsToDictionary("ABDNOQRSÀÁÂÃÄÅÐÑÒÓÔÕÖØĂĄĎĐŃŅŇŌŎŐŔŖŘŚŜŞŠȘЅЊЖф□", 21);
- AddCharsToDictionary("љ", 22);
- AddCharsToDictionary("ю", 23);
- AddCharsToDictionary("%IJЫ", 24);
- AddCharsToDictionary("@©®мшњ", 25);
- AddCharsToDictionary("MМШ", 26);
- AddCharsToDictionary("mw¼ŵЮщ", 27);
- AddCharsToDictionary("¾æœЉ", 28);
- AddCharsToDictionary("½Щ", 29);
- AddCharsToDictionary("™", 30);
- AddCharsToDictionary("WÆŒŴ—…‰", 31);
- AddCharsToDictionary("", 32);
- AddCharsToDictionary("", 34);
- AddCharsToDictionary("", 40);
- AddCharsToDictionary("", 41);
- }
- static private void AddCharsToDictionary(string chars, float charSize)
- {
- for (int i = 0; i < chars.Length; i++)
- {
- if (!charWidth.ContainsKey(chars[i]))
- charWidth.Add(chars[i], charSize);
- }
- }
- static public float TotalWidth(string text)
- {
- float width = 0;
- float w;
- foreach (char c in text)
- {
- if (charWidth.TryGetValue(c, out w))
- width += charWidth[c] + 1; //add 1 for space betwixt chars
- else
- width += 18; //some middleing value 18
- }
- if (width > 1)
- return width - 1; //remove 1 for trailing space
- else
- return 0;
- }
- static public string Center(string text, float fontSize = 1.0f, float screenWidth = 1)
- {
- text = text.Trim();
- screenWidth = (screenWidth * singlePanelWidth) / fontSize; //screen width in px
- float margin = 2 * fontSize;
- float textWidth = TotalWidth(text);
- float paddingwidth = (screenWidth - textWidth) / 2 - margin;
- float paddingcharwidth = charWidth[PADDING_CHAR] + 1;
- int paddingchars = (int)Math.Round(paddingwidth / paddingcharwidth, MidpointRounding.AwayFromZero); // +1
- if (paddingchars < 1)
- return text;
- string s = new string(PADDING_CHAR, paddingchars);
- return s + text;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement