using System;
using System.Text;

namespace SharpQuake
{
	internal static class sbar
	{
		public static int Lines { get; set; }

		public static void Init()
		{
			for (int i = 0; i < 10; i++)
			{
				string str = i.ToString();
				sbar._Nums[0, i] = Drawer.PicFromWad("num_" + str);
				sbar._Nums[1, i] = Drawer.PicFromWad("anum_" + str);
			}
			sbar._Nums[0, 10] = Drawer.PicFromWad("num_minus");
			sbar._Nums[1, 10] = Drawer.PicFromWad("anum_minus");
			sbar._Colon = Drawer.PicFromWad("num_colon");
			sbar._Slash = Drawer.PicFromWad("num_slash");
			sbar._Weapons[0, 0] = Drawer.PicFromWad("inv_shotgun");
			sbar._Weapons[0, 1] = Drawer.PicFromWad("inv_sshotgun");
			sbar._Weapons[0, 2] = Drawer.PicFromWad("inv_nailgun");
			sbar._Weapons[0, 3] = Drawer.PicFromWad("inv_snailgun");
			sbar._Weapons[0, 4] = Drawer.PicFromWad("inv_rlaunch");
			sbar._Weapons[0, 5] = Drawer.PicFromWad("inv_srlaunch");
			sbar._Weapons[0, 6] = Drawer.PicFromWad("inv_lightng");
			sbar._Weapons[1, 0] = Drawer.PicFromWad("inv2_shotgun");
			sbar._Weapons[1, 1] = Drawer.PicFromWad("inv2_sshotgun");
			sbar._Weapons[1, 2] = Drawer.PicFromWad("inv2_nailgun");
			sbar._Weapons[1, 3] = Drawer.PicFromWad("inv2_snailgun");
			sbar._Weapons[1, 4] = Drawer.PicFromWad("inv2_rlaunch");
			sbar._Weapons[1, 5] = Drawer.PicFromWad("inv2_srlaunch");
			sbar._Weapons[1, 6] = Drawer.PicFromWad("inv2_lightng");
			for (int j = 0; j < 5; j++)
			{
				string str2 = "inva" + (j + 1).ToString();
				sbar._Weapons[2 + j, 0] = Drawer.PicFromWad(str2 + "_shotgun");
				sbar._Weapons[2 + j, 1] = Drawer.PicFromWad(str2 + "_sshotgun");
				sbar._Weapons[2 + j, 2] = Drawer.PicFromWad(str2 + "_nailgun");
				sbar._Weapons[2 + j, 3] = Drawer.PicFromWad(str2 + "_snailgun");
				sbar._Weapons[2 + j, 4] = Drawer.PicFromWad(str2 + "_rlaunch");
				sbar._Weapons[2 + j, 5] = Drawer.PicFromWad(str2 + "_srlaunch");
				sbar._Weapons[2 + j, 6] = Drawer.PicFromWad(str2 + "_lightng");
			}
			sbar._Ammo[0] = Drawer.PicFromWad("sb_shells");
			sbar._Ammo[1] = Drawer.PicFromWad("sb_nails");
			sbar._Ammo[2] = Drawer.PicFromWad("sb_rocket");
			sbar._Ammo[3] = Drawer.PicFromWad("sb_cells");
			sbar._Armor[0] = Drawer.PicFromWad("sb_armor1");
			sbar._Armor[1] = Drawer.PicFromWad("sb_armor2");
			sbar._Armor[2] = Drawer.PicFromWad("sb_armor3");
			sbar._Items[0] = Drawer.PicFromWad("sb_key1");
			sbar._Items[1] = Drawer.PicFromWad("sb_key2");
			sbar._Items[2] = Drawer.PicFromWad("sb_invis");
			sbar._Items[3] = Drawer.PicFromWad("sb_invuln");
			sbar._Items[4] = Drawer.PicFromWad("sb_suit");
			sbar._Items[5] = Drawer.PicFromWad("sb_quad");
			sbar._Sigil[0] = Drawer.PicFromWad("sb_sigil1");
			sbar._Sigil[1] = Drawer.PicFromWad("sb_sigil2");
			sbar._Sigil[2] = Drawer.PicFromWad("sb_sigil3");
			sbar._Sigil[3] = Drawer.PicFromWad("sb_sigil4");
			sbar._Faces[4, 0] = Drawer.PicFromWad("face1");
			sbar._Faces[4, 1] = Drawer.PicFromWad("face_p1");
			sbar._Faces[3, 0] = Drawer.PicFromWad("face2");
			sbar._Faces[3, 1] = Drawer.PicFromWad("face_p2");
			sbar._Faces[2, 0] = Drawer.PicFromWad("face3");
			sbar._Faces[2, 1] = Drawer.PicFromWad("face_p3");
			sbar._Faces[1, 0] = Drawer.PicFromWad("face4");
			sbar._Faces[1, 1] = Drawer.PicFromWad("face_p4");
			sbar._Faces[0, 0] = Drawer.PicFromWad("face5");
			sbar._Faces[0, 1] = Drawer.PicFromWad("face_p5");
			sbar._FaceInvis = Drawer.PicFromWad("face_invis");
			sbar._FaceInvuln = Drawer.PicFromWad("face_invul2");
			sbar._FaceInvisInvuln = Drawer.PicFromWad("face_inv2");
			sbar._FaceQuad = Drawer.PicFromWad("face_quad");
			cmd.Add("+showscores", new xcommand_t(sbar.ShowScores));
			cmd.Add("-showscores", new xcommand_t(sbar.DontShowScores));
			sbar._SBar = Drawer.PicFromWad("sbar");
			sbar._IBar = Drawer.PicFromWad("ibar");
			sbar._ScoreBar = Drawer.PicFromWad("scorebar");
			if (common.GameKind == GameKind.Hipnotic)
			{
				sbar._HWeapons[0, 0] = Drawer.PicFromWad("inv_laser");
				sbar._HWeapons[0, 1] = Drawer.PicFromWad("inv_mjolnir");
				sbar._HWeapons[0, 2] = Drawer.PicFromWad("inv_gren_prox");
				sbar._HWeapons[0, 3] = Drawer.PicFromWad("inv_prox_gren");
				sbar._HWeapons[0, 4] = Drawer.PicFromWad("inv_prox");
				sbar._HWeapons[1, 0] = Drawer.PicFromWad("inv2_laser");
				sbar._HWeapons[1, 1] = Drawer.PicFromWad("inv2_mjolnir");
				sbar._HWeapons[1, 2] = Drawer.PicFromWad("inv2_gren_prox");
				sbar._HWeapons[1, 3] = Drawer.PicFromWad("inv2_prox_gren");
				sbar._HWeapons[1, 4] = Drawer.PicFromWad("inv2_prox");
				for (int k = 0; k < 5; k++)
				{
					string str3 = "inva" + (k + 1).ToString();
					sbar._HWeapons[2 + k, 0] = Drawer.PicFromWad(str3 + "_laser");
					sbar._HWeapons[2 + k, 1] = Drawer.PicFromWad(str3 + "_mjolnir");
					sbar._HWeapons[2 + k, 2] = Drawer.PicFromWad(str3 + "_gren_prox");
					sbar._HWeapons[2 + k, 3] = Drawer.PicFromWad(str3 + "_prox_gren");
					sbar._HWeapons[2 + k, 4] = Drawer.PicFromWad(str3 + "_prox");
				}
				sbar._HItems[0] = Drawer.PicFromWad("sb_wsuit");
				sbar._HItems[1] = Drawer.PicFromWad("sb_eshld");
			}
			if (common.GameKind == GameKind.Rogue)
			{
				sbar._RInvBar[0] = Drawer.PicFromWad("r_invbar1");
				sbar._RInvBar[1] = Drawer.PicFromWad("r_invbar2");
				sbar._RWeapons[0] = Drawer.PicFromWad("r_lava");
				sbar._RWeapons[1] = Drawer.PicFromWad("r_superlava");
				sbar._RWeapons[2] = Drawer.PicFromWad("r_gren");
				sbar._RWeapons[3] = Drawer.PicFromWad("r_multirock");
				sbar._RWeapons[4] = Drawer.PicFromWad("r_plasma");
				sbar._RItems[0] = Drawer.PicFromWad("r_shield1");
				sbar._RItems[1] = Drawer.PicFromWad("r_agrav1");
				sbar._RTeamBord = Drawer.PicFromWad("r_teambord");
				sbar._RAmmo[0] = Drawer.PicFromWad("r_ammolava");
				sbar._RAmmo[1] = Drawer.PicFromWad("r_ammomulti");
				sbar._RAmmo[2] = Drawer.PicFromWad("r_ammoplasma");
			}
		}

		public static void Changed()
		{
			sbar._Updates = 0;
		}

		public static void Draw()
		{
			viddef_t vid = Scr.vid;
			if (Scr.ConCurrent == (float)vid.height)
			{
				return;
			}
			if (sbar._Updates >= vid.numpages)
			{
				return;
			}
			Scr.CopyEverithing = true;
			sbar._Updates++;
			if (sbar.Lines > 0 && vid.width > 320)
			{
				Drawer.TileClear(0, vid.height - sbar.Lines, vid.width, sbar.Lines);
			}
			if (sbar.Lines > 24)
			{
				sbar.DrawInventory();
				if (client.cl.maxclients != 1)
				{
					sbar.DrawFrags();
				}
			}
			client_state_t cl = client.cl;
			if (sbar._ShowScores || cl.stats[QStats.STAT_HEALTH] <= 0)
			{
				sbar.DrawPic(0, 0, sbar._ScoreBar);
				sbar.DrawScoreboard();
				sbar._Updates = 0;
			}
			else if (sbar.Lines > 0)
			{
				sbar.DrawPic(0, 0, sbar._SBar);
				if (common.GameKind == GameKind.Hipnotic)
				{
					if (cl.HasItems(QItems.IT_KEY1))
					{
						sbar.DrawPic(209, 3, sbar._Items[0]);
					}
					if (cl.HasItems(QItems.IT_KEY2))
					{
						sbar.DrawPic(209, 12, sbar._Items[1]);
					}
				}
				if (cl.HasItems(QItems.IT_INVULNERABILITY))
				{
					sbar.DrawNum(24, 0, 666, 3, 1);
					sbar.DrawPic(0, 0, Drawer.Disc);
				}
				else if (common.GameKind == GameKind.Rogue)
				{
					sbar.DrawNum(24, 0, cl.stats[QStats.STAT_ARMOR], 3, (cl.stats[QStats.STAT_ARMOR] <= 25) ? 1 : 0);
					if (cl.HasItems(QItems.RIT_ARMOR3))
					{
						sbar.DrawPic(0, 0, sbar._Armor[2]);
					}
					else if (cl.HasItems(QItems.RIT_ARMOR2))
					{
						sbar.DrawPic(0, 0, sbar._Armor[1]);
					}
					else if (cl.HasItems(QItems.RIT_ARMOR1))
					{
						sbar.DrawPic(0, 0, sbar._Armor[0]);
					}
				}
				else
				{
					sbar.DrawNum(24, 0, cl.stats[QStats.STAT_ARMOR], 3, (cl.stats[QStats.STAT_ARMOR] <= 25) ? 1 : 0);
					if (cl.HasItems(QItems.IT_ARMOR3))
					{
						sbar.DrawPic(0, 0, sbar._Armor[2]);
					}
					else if (cl.HasItems(QItems.IT_ARMOR2))
					{
						sbar.DrawPic(0, 0, sbar._Armor[1]);
					}
					else if (cl.HasItems(QItems.IT_ARMOR1))
					{
						sbar.DrawPic(0, 0, sbar._Armor[0]);
					}
				}
				sbar.DrawFace();
				sbar.DrawNum(136, 0, cl.stats[QStats.STAT_HEALTH], 3, (cl.stats[QStats.STAT_HEALTH] <= 25) ? 1 : 0);
				if (common.GameKind == GameKind.Rogue)
				{
					if (cl.HasItems(QItems.RIT_SHELLS))
					{
						sbar.DrawPic(224, 0, sbar._Ammo[0]);
					}
					else if (cl.HasItems(QItems.RIT_NAILS))
					{
						sbar.DrawPic(224, 0, sbar._Ammo[1]);
					}
					else if (cl.HasItems(QItems.RIT_ROCKETS))
					{
						sbar.DrawPic(224, 0, sbar._Ammo[2]);
					}
					else if (cl.HasItems(QItems.RIT_CELLS))
					{
						sbar.DrawPic(224, 0, sbar._Ammo[3]);
					}
					else if (cl.HasItems(QItems.RIT_LAVA_NAILS))
					{
						sbar.DrawPic(224, 0, sbar._RAmmo[0]);
					}
					else if (cl.HasItems(QItems.RIT_PLASMA_AMMO))
					{
						sbar.DrawPic(224, 0, sbar._RAmmo[1]);
					}
					else if (cl.HasItems(QItems.RIT_MULTI_ROCKETS))
					{
						sbar.DrawPic(224, 0, sbar._RAmmo[2]);
					}
				}
				else if (cl.HasItems(QItems.IT_SHELLS))
				{
					sbar.DrawPic(224, 0, sbar._Ammo[0]);
				}
				else if (cl.HasItems(QItems.IT_NAILS))
				{
					sbar.DrawPic(224, 0, sbar._Ammo[1]);
				}
				else if (cl.HasItems(QItems.IT_ROCKETS))
				{
					sbar.DrawPic(224, 0, sbar._Ammo[2]);
				}
				else if (cl.HasItems(QItems.IT_CELLS))
				{
					sbar.DrawPic(224, 0, sbar._Ammo[3]);
				}
				sbar.DrawNum(248, 0, cl.stats[QStats.STAT_AMMO], 3, (cl.stats[QStats.STAT_AMMO] <= 10) ? 1 : 0);
			}
			if (vid.width > 320 && client.cl.gametype == 1)
			{
				sbar.MiniDeathmatchOverlay();
			}
		}

		public static void IntermissionOverlay()
		{
			Scr.CopyEverithing = true;
			Scr.FullUpdate = 0;
			if (client.cl.gametype == 1)
			{
				sbar.DeathmatchOverlay();
				return;
			}
			glpic_t pic = Drawer.CachePic("gfx/complete.lmp");
			Drawer.DrawPic(64, 24, pic);
			pic = Drawer.CachePic("gfx/inter.lmp");
			Drawer.DrawTransPic(0, 56, pic);
			int num = client.cl.completed_time / 60;
			sbar.IntermissionNumber(160, 64, num, 3, 0);
			int num2 = client.cl.completed_time - num * 60;
			Drawer.DrawTransPic(234, 64, sbar._Colon);
			Drawer.DrawTransPic(246, 64, sbar._Nums[0, num2 / 10]);
			Drawer.DrawTransPic(266, 64, sbar._Nums[0, num2 % 10]);
			sbar.IntermissionNumber(160, 104, client.cl.stats[QStats.STAT_SECRETS], 3, 0);
			Drawer.DrawTransPic(232, 104, sbar._Slash);
			sbar.IntermissionNumber(240, 104, client.cl.stats[QStats.STAT_TOTALSECRETS], 3, 0);
			sbar.IntermissionNumber(160, 144, client.cl.stats[QStats.STAT_MONSTERS], 3, 0);
			Drawer.DrawTransPic(232, 144, sbar._Slash);
			sbar.IntermissionNumber(240, 144, client.cl.stats[QStats.STAT_TOTALMONSTERS], 3, 0);
		}

		public static void FinaleOverlay()
		{
			Scr.CopyEverithing = true;
			glpic_t glpic_t = Drawer.CachePic("gfx/finale.lmp");
			Drawer.DrawTransPic((Scr.vid.width - glpic_t.width) / 2, 16, glpic_t);
		}

		private static void IntermissionNumber(int x, int y, int num, int digits, int color)
		{
			string text = num.ToString();
			if (text.Length > digits)
			{
				text = text.Remove(0, text.Length - digits);
			}
			if (text.Length < digits)
			{
				x += (digits - text.Length) * 24;
			}
			for (int i = 0; i < text.Length; i++)
			{
				int num2 = (int)((text[i] == '-') ? '\n' : (text[i] - '0'));
				Drawer.DrawTransPic(x, y, sbar._Nums[color, num2]);
				x += 24;
			}
		}

		private static void DrawInventory()
		{
			client_state_t cl = client.cl;
			if (common.GameKind == GameKind.Rogue)
			{
				if (cl.stats[QStats.STAT_ACTIVEWEAPON] >= QItems.RIT_LAVA_NAILGUN)
				{
					sbar.DrawPic(0, -24, sbar._RInvBar[0]);
				}
				else
				{
					sbar.DrawPic(0, -24, sbar._RInvBar[1]);
				}
			}
			else
			{
				sbar.DrawPic(0, -24, sbar._IBar);
			}
			int num2;
			for (int i = 0; i < 7; i++)
			{
				if (cl.HasItems(QItems.IT_SHOTGUN << i))
				{
					float num = cl.item_gettime[i];
					num2 = (int)((cl.time - (double)num) * 10.0);
					if (num2 >= 10)
					{
						if (cl.stats[QStats.STAT_ACTIVEWEAPON] == QItems.IT_SHOTGUN << i)
						{
							num2 = 1;
						}
						else
						{
							num2 = 0;
						}
					}
					else
					{
						num2 = num2 % 5 + 2;
					}
					sbar.DrawPic(i * 24, -16, sbar._Weapons[num2, i]);
					if (num2 > 1)
					{
						sbar._Updates = 0;
					}
				}
			}
			if (common.GameKind == GameKind.Hipnotic)
			{
				int num3 = 0;
				for (int j = 0; j < 4; j++)
				{
					if (cl.HasItems(1 << sbar._HipWeapons[j]))
					{
						float num4 = cl.item_gettime[sbar._HipWeapons[j]];
						num2 = (int)((cl.time - (double)num4) * 10.0);
						if (num2 >= 10)
						{
							if (cl.stats[QStats.STAT_ACTIVEWEAPON] == 1 << sbar._HipWeapons[j])
							{
								num2 = 1;
							}
							else
							{
								num2 = 0;
							}
						}
						else
						{
							num2 = num2 % 5 + 2;
						}
						if (j == 2)
						{
							if (cl.HasItems(QItems.HIT_PROXIMITY_GUN) && num2 > 0)
							{
								num3 = 1;
								sbar.DrawPic(96, -16, sbar._HWeapons[num2, 2]);
							}
						}
						else if (j == 3)
						{
							if (cl.HasItems(QItems.IT_SHOTGUN << 4))
							{
								if (num2 > 0 && num3 == 0)
								{
									sbar.DrawPic(96, -16, sbar._HWeapons[num2, 3]);
								}
								else if (num3 == 0)
								{
									sbar.DrawPic(96, -16, sbar._HWeapons[0, 3]);
								}
							}
							else
							{
								sbar.DrawPic(96, -16, sbar._HWeapons[num2, 4]);
							}
						}
						else
						{
							sbar.DrawPic(176 + j * 24, -16, sbar._HWeapons[num2, j]);
						}
						if (num2 > 1)
						{
							sbar._Updates = 0;
						}
					}
				}
			}
			if (common.GameKind == GameKind.Rogue && cl.stats[QStats.STAT_ACTIVEWEAPON] >= QItems.RIT_LAVA_NAILGUN)
			{
				for (int k = 0; k < 5; k++)
				{
					if (cl.stats[QStats.STAT_ACTIVEWEAPON] == QItems.RIT_LAVA_NAILGUN << k)
					{
						sbar.DrawPic((k + 2) * 24, -16, sbar._RWeapons[k]);
					}
				}
			}
			for (int l = 0; l < 4; l++)
			{
				string text = cl.stats[QStats.STAT_SHELLS + l].ToString().PadLeft(3);
				if (text[0] != ' ')
				{
					sbar.DrawCharacter((6 * l + 1) * 8 - 2, -24, (int)('\u0012' + text[0] - '0'));
				}
				if (text[1] != ' ')
				{
					sbar.DrawCharacter((6 * l + 2) * 8 - 2, -24, (int)('\u0012' + text[1] - '0'));
				}
				if (text[2] != ' ')
				{
					sbar.DrawCharacter((6 * l + 3) * 8 - 2, -24, (int)('\u0012' + text[2] - '0'));
				}
			}
			num2 = 0;
			for (int m = 0; m < 6; m++)
			{
				if (cl.HasItems(1 << 17 + m))
				{
					float num5 = cl.item_gettime[17 + m];
					if (num5 > 0f && (double)num5 > cl.time - 2.0 && num2 > 0)
					{
						sbar._Updates = 0;
					}
					else if (common.GameKind != GameKind.Hipnotic || m > 1)
					{
						sbar.DrawPic(192 + m * 16, -16, sbar._Items[m]);
					}
					if (num5 > 0f && (double)num5 > cl.time - 2.0)
					{
						sbar._Updates = 0;
					}
				}
			}
			if (common.GameKind == GameKind.Hipnotic)
			{
				for (int n = 0; n < 2; n++)
				{
					if (cl.HasItems(1 << 24 + n))
					{
						float num6 = cl.item_gettime[24 + n];
						if (num6 > 0f && (double)num6 > cl.time - 2.0 && num2 > 0)
						{
							sbar._Updates = 0;
						}
						else
						{
							sbar.DrawPic(288 + n * 16, -16, sbar._HItems[n]);
						}
						if (num6 > 0f && (double)num6 > cl.time - 2.0)
						{
							sbar._Updates = 0;
						}
					}
				}
			}
			if (common.GameKind == GameKind.Rogue)
			{
				for (int num7 = 0; num7 < 2; num7++)
				{
					if (cl.HasItems(1 << 29 + num7))
					{
						float num8 = cl.item_gettime[29 + num7];
						if (num8 > 0f && (double)num8 > cl.time - 2.0 && num2 > 0)
						{
							sbar._Updates = 0;
						}
						else
						{
							sbar.DrawPic(288 + num7 * 16, -16, sbar._RItems[num7]);
						}
						if (num8 > 0f && (double)num8 > cl.time - 2.0)
						{
							sbar._Updates = 0;
						}
					}
				}
				return;
			}
			for (int num9 = 0; num9 < 4; num9++)
			{
				if (cl.HasItems(1 << 28 + num9))
				{
					float num10 = cl.item_gettime[28 + num9];
					if (num10 > 0f && (double)num10 > cl.time - 2.0 && num2 > 0)
					{
						sbar._Updates = 0;
					}
					else
					{
						sbar.DrawPic(288 + num9 * 8, -16, sbar._Sigil[num9]);
					}
					if (num10 > 0f && (double)num10 > cl.time - 2.0)
					{
						sbar._Updates = 0;
					}
				}
			}
		}

		private static void DrawFrags()
		{
			sbar.SortFrags();
			int num = (sbar._ScoreBoardLines <= 4) ? sbar._ScoreBoardLines : 4;
			int num2 = 23;
			client_state_t cl = client.cl;
			int num3;
			if (cl.gametype == 1)
			{
				num3 = 0;
			}
			else
			{
				num3 = Scr.vid.width - 320 >> 1;
			}
			int num4 = Scr.vid.height - 24 - 23;
			for (int i = 0; i < num; i++)
			{
				int num5 = sbar._FragSort[i];
				scoreboard_t scoreboard_t = cl.scores[num5];
				if (!string.IsNullOrEmpty(scoreboard_t.name))
				{
					int num6 = scoreboard_t.colors & 240;
					int num7 = (scoreboard_t.colors & 15) << 4;
					num6 = sbar.ColorForMap(num6);
					num7 = sbar.ColorForMap(num7);
					Drawer.Fill(num3 + num2 * 8 + 10, num4, 28, 4, num6);
					Drawer.Fill(num3 + num2 * 8 + 10, num4 + 4, 28, 3, num7);
					int frags = scoreboard_t.frags;
					string text = frags.ToString().PadLeft(3);
					sbar.DrawCharacter((num2 + 1) * 8, -24, (int)text[0]);
					sbar.DrawCharacter((num2 + 2) * 8, -24, (int)text[1]);
					sbar.DrawCharacter((num2 + 3) * 8, -24, (int)text[2]);
					if (num5 == cl.viewentity - 1)
					{
						sbar.DrawCharacter(num2 * 8 + 2, -24, 16);
						sbar.DrawCharacter((num2 + 4) * 8 - 4, -24, 17);
					}
					num2 += 4;
				}
			}
		}

		private static void DrawPic(int x, int y, glpic_t pic)
		{
			if (client.cl.gametype == 1)
			{
				Drawer.DrawPic(x, y + (Scr.vid.height - 24), pic);
				return;
			}
			Drawer.DrawPic(x + (Scr.vid.width - 320 >> 1), y + (Scr.vid.height - 24), pic);
		}

		private static void DrawScoreboard()
		{
			sbar.SoloScoreboard();
			if (client.cl.gametype == 1)
			{
				sbar.DeathmatchOverlay();
			}
		}

		private static void DrawNum(int x, int y, int num, int digits, int color)
		{
			string text = num.ToString();
			if (text.Length > digits)
			{
				text = text.Remove(text.Length - digits);
			}
			if (text.Length < digits)
			{
				x += (digits - text.Length) * 24;
			}
			for (int i = 0; i < text.Length; i++)
			{
				int num2;
				if (text[i] == '-')
				{
					num2 = 10;
				}
				else
				{
					num2 = (int)(text[i] - '0');
				}
				sbar.DrawTransPic(x, y, sbar._Nums[color, num2]);
				x += 24;
			}
		}

		private static void DrawFace()
		{
			client_state_t cl = client.cl;
			if (common.GameKind == GameKind.Rogue && client.cl.maxclients != 1 && host.TeamPlay > 3f && host.TeamPlay < 7f)
			{
				scoreboard_t scoreboard_t = cl.scores[cl.viewentity - 1];
				int num = scoreboard_t.colors & 240;
				int num2 = (scoreboard_t.colors & 15) << 4;
				num = sbar.ColorForMap(num);
				num2 = sbar.ColorForMap(num2);
				int x;
				if (cl.gametype == 1)
				{
					x = 113;
				}
				else
				{
					x = (Scr.vid.width - 320 >> 1) + 113;
				}
				sbar.DrawPic(112, 0, sbar._RTeamBord);
				Drawer.Fill(x, Scr.vid.height - 24 + 3, 22, 9, num);
				Drawer.Fill(x, Scr.vid.height - 24 + 12, 22, 9, num2);
				string text = scoreboard_t.frags.ToString().PadLeft(3);
				if (num == 8)
				{
					if (text[0] != ' ')
					{
						sbar.DrawCharacter(109, 3, (int)('\u0012' + text[0] - '0'));
					}
					if (text[1] != ' ')
					{
						sbar.DrawCharacter(116, 3, (int)('\u0012' + text[1] - '0'));
					}
					if (text[2] != ' ')
					{
						sbar.DrawCharacter(123, 3, (int)('\u0012' + text[2] - '0'));
						return;
					}
				}
				else
				{
					sbar.DrawCharacter(109, 3, (int)text[0]);
					sbar.DrawCharacter(116, 3, (int)text[1]);
					sbar.DrawCharacter(123, 3, (int)text[2]);
				}
				return;
			}
			if (cl.HasItems(QItems.IT_INVISIBILITY | QItems.IT_INVULNERABILITY))
			{
				sbar.DrawPic(112, 0, sbar._FaceInvisInvuln);
				return;
			}
			if (cl.HasItems(QItems.IT_QUAD))
			{
				sbar.DrawPic(112, 0, sbar._FaceQuad);
				return;
			}
			if (cl.HasItems(QItems.IT_INVISIBILITY))
			{
				sbar.DrawPic(112, 0, sbar._FaceInvis);
				return;
			}
			if (cl.HasItems(QItems.IT_INVULNERABILITY))
			{
				sbar.DrawPic(112, 0, sbar._FaceInvuln);
				return;
			}
			int num3;
			if (cl.stats[QStats.STAT_HEALTH] >= 100)
			{
				num3 = 4;
			}
			else
			{
				num3 = cl.stats[QStats.STAT_HEALTH] / 20;
			}
			int num4;
			if (cl.time <= (double)cl.faceanimtime)
			{
				num4 = 1;
				sbar._Updates = 0;
			}
			else
			{
				num4 = 0;
			}
			sbar.DrawPic(112, 0, sbar._Faces[num3, num4]);
		}

		private static void MiniDeathmatchOverlay()
		{
			if (Scr.vid.width < 512 || sbar.Lines == 0)
			{
				return;
			}
			Scr.CopyEverithing = true;
			Scr.FullUpdate = 0;
			sbar.SortFrags();
			int scoreBoardLines = sbar._ScoreBoardLines;
			int num = Scr.vid.height - sbar.Lines;
			int num2 = sbar.Lines / 8;
			if (num2 < 3)
			{
				return;
			}
			int num3 = 0;
			while (num3 < sbar._ScoreBoardLines && sbar._FragSort[num3] != client.cl.viewentity - 1)
			{
				num3++;
			}
			if (num3 == sbar._ScoreBoardLines)
			{
				num3 = 0;
			}
			else
			{
				num3 -= num2 / 2;
			}
			if (num3 > sbar._ScoreBoardLines - num2)
			{
				num3 = sbar._ScoreBoardLines - num2;
			}
			if (num3 < 0)
			{
				num3 = 0;
			}
			int num4 = 324;
			while (num3 < sbar._ScoreBoardLines && num < Scr.vid.height - 8)
			{
				int num5 = sbar._FragSort[num3];
				scoreboard_t scoreboard_t = client.cl.scores[num5];
				if (!string.IsNullOrEmpty(scoreboard_t.name))
				{
					int num6 = scoreboard_t.colors & 240;
					int num7 = (scoreboard_t.colors & 15) << 4;
					num6 = sbar.ColorForMap(num6);
					num7 = sbar.ColorForMap(num7);
					Drawer.Fill(num4, num + 1, 40, 3, num6);
					Drawer.Fill(num4, num + 4, 40, 4, num7);
					string text = scoreboard_t.frags.ToString().PadLeft(3);
					Drawer.DrawCharacter(num4 + 8, num, (int)text[0]);
					Drawer.DrawCharacter(num4 + 16, num, (int)text[1]);
					Drawer.DrawCharacter(num4 + 24, num, (int)text[2]);
					if (num5 == client.cl.viewentity - 1)
					{
						Drawer.DrawCharacter(num4, num, 16);
						Drawer.DrawCharacter(num4 + 32, num, 17);
					}
					Drawer.DrawString(num4 + 48, num, scoreboard_t.name);
					num += 8;
				}
				num3++;
			}
		}

		private static void SortFrags()
		{
			client_state_t cl = client.cl;
			sbar._ScoreBoardLines = 0;
			for (int i = 0; i < cl.maxclients; i++)
			{
				if (!string.IsNullOrEmpty(cl.scores[i].name))
				{
					sbar._FragSort[sbar._ScoreBoardLines] = i;
					sbar._ScoreBoardLines++;
				}
			}
			for (int j = 0; j < sbar._ScoreBoardLines; j++)
			{
				for (int k = 0; k < sbar._ScoreBoardLines - 1 - j; k++)
				{
					if (cl.scores[sbar._FragSort[k]].frags < cl.scores[sbar._FragSort[k + 1]].frags)
					{
						int num = sbar._FragSort[k];
						sbar._FragSort[k] = sbar._FragSort[k + 1];
						sbar._FragSort[k + 1] = num;
					}
				}
			}
		}

		private static void DrawCharacter(int x, int y, int num)
		{
			if (client.cl.gametype == 1)
			{
				Drawer.DrawCharacter(x + 4, y + Scr.vid.height - 24, num);
				return;
			}
			Drawer.DrawCharacter(x + (Scr.vid.width - 320 >> 1) + 4, y + Scr.vid.height - 24, num);
		}

		private static int ColorForMap(int m)
		{
			if (m >= 128)
			{
				return m + 8;
			}
			return m + 8;
		}

		private static void SoloScoreboard()
		{
			StringBuilder stringBuilder = new StringBuilder(80);
			client_state_t cl = client.cl;
			stringBuilder.AppendFormat("Monsters:{0,3:d} /{1,3:d}", cl.stats[QStats.STAT_MONSTERS], client.cl.stats[QStats.STAT_TOTALMONSTERS]);
			sbar.DrawString(8, 4, stringBuilder.ToString());
			stringBuilder.Length = 0;
			stringBuilder.AppendFormat("Secrets :{0,3:d} /{1,3:d}", cl.stats[QStats.STAT_SECRETS], cl.stats[QStats.STAT_TOTALSECRETS]);
			sbar.DrawString(8, 12, stringBuilder.ToString());
			int num = (int)(cl.time / 60.0);
			int num2 = (int)(cl.time - (double)(60 * num));
			int num3 = num2 / 10;
			int num4 = num2 - 10 * num3;
			stringBuilder.Length = 0;
			stringBuilder.AppendFormat("Time :{0,3}:{1}{2}", num, num3, num4);
			sbar.DrawString(184, 4, stringBuilder.ToString());
			int length = cl.levelname.Length;
			sbar.DrawString(232 - length * 4, 12, cl.levelname);
		}

		private static void DeathmatchOverlay()
		{
			Scr.CopyEverithing = true;
			Scr.FullUpdate = 0;
			glpic_t glpic_t = Drawer.CachePic("gfx/ranking.lmp");
			menu.DrawPic((320 - glpic_t.width) / 2, 8, glpic_t);
			sbar.SortFrags();
			int scoreBoardLines = sbar._ScoreBoardLines;
			int num = 80 + (Scr.vid.width - 320 >> 1);
			int num2 = 40;
			for (int i = 0; i < scoreBoardLines; i++)
			{
				int num3 = sbar._FragSort[i];
				scoreboard_t scoreboard_t = client.cl.scores[num3];
				if (!string.IsNullOrEmpty(scoreboard_t.name))
				{
					int num4 = scoreboard_t.colors & 240;
					int num5 = (scoreboard_t.colors & 15) << 4;
					num4 = sbar.ColorForMap(num4);
					num5 = sbar.ColorForMap(num5);
					Drawer.Fill(num, num2, 40, 4, num4);
					Drawer.Fill(num, num2 + 4, 40, 4, num5);
					string text = scoreboard_t.frags.ToString().PadLeft(3);
					Drawer.DrawCharacter(num + 8, num2, (int)text[0]);
					Drawer.DrawCharacter(num + 16, num2, (int)text[1]);
					Drawer.DrawCharacter(num + 24, num2, (int)text[2]);
					if (num3 == client.cl.viewentity - 1)
					{
						Drawer.DrawCharacter(num - 8, num2, 12);
					}
					Drawer.DrawString(num + 64, num2, scoreboard_t.name);
					num2 += 10;
				}
			}
		}

		private static void DrawTransPic(int x, int y, glpic_t pic)
		{
			if (client.cl.gametype == 1)
			{
				Drawer.DrawTransPic(x, y + (Scr.vid.height - 24), pic);
				return;
			}
			Drawer.DrawTransPic(x + (Scr.vid.width - 320 >> 1), y + (Scr.vid.height - 24), pic);
		}

		private static void DrawString(int x, int y, string str)
		{
			if (client.cl.gametype == 1)
			{
				Drawer.DrawString(x, y + Scr.vid.height - 24, str);
				return;
			}
			Drawer.DrawString(x + (Scr.vid.width - 320 >> 1), y + Scr.vid.height - 24, str);
		}

		private static void ShowScores()
		{
			if (sbar._ShowScores)
			{
				return;
			}
			sbar._ShowScores = true;
			sbar._Updates = 0;
		}

		private static void DontShowScores()
		{
			sbar._ShowScores = false;
			sbar._Updates = 0;
		}

		public const int SBAR_HEIGHT = 24;

		private const int STAT_MINUS = 10;

		private static int _Updates;

		private static bool _ShowScores;

		private static glpic_t[,] _Nums = new glpic_t[2, 11];

		private static glpic_t _Colon;

		private static glpic_t _Slash;

		private static glpic_t _IBar;

		private static glpic_t _SBar;

		private static glpic_t _ScoreBar;

		private static glpic_t[,] _Weapons = new glpic_t[7, 8];

		private static glpic_t[] _Ammo = new glpic_t[4];

		private static glpic_t[] _Sigil = new glpic_t[4];

		private static glpic_t[] _Armor = new glpic_t[3];

		private static glpic_t[] _Items = new glpic_t[32];

		private static glpic_t[,] _Faces = new glpic_t[7, 2];

		private static glpic_t _FaceInvis;

		private static glpic_t _FaceQuad;

		private static glpic_t _FaceInvuln;

		private static glpic_t _FaceInvisInvuln;

		private static glpic_t[] _RInvBar = new glpic_t[2];

		private static glpic_t[] _RWeapons = new glpic_t[5];

		private static glpic_t[] _RItems = new glpic_t[2];

		private static glpic_t[] _RAmmo = new glpic_t[3];

		private static glpic_t _RTeamBord;

		private static glpic_t[,] _HWeapons = new glpic_t[7, 5];

		private static int[] _HipWeapons = new int[]
		{
			QItems.HIT_LASER_CANNON_BIT,
			QItems.HIT_MJOLNIR_BIT,
			4,
			QItems.HIT_PROXIMITY_GUN_BIT
		};

		private static glpic_t[] _HItems = new glpic_t[2];

		private static int[] _FragSort = new int[16];

		private static string[] _ScoreBoardText = new string[16];

		private static int[] _ScoreBoardTop = new int[16];

		private static int[] _ScoreBoardBottom = new int[16];

		private static int[] _ScoreBoardCount = new int[16];

		private static int _ScoreBoardLines;
	}
}
