using System;

public class CMap
{
	private const sbyte HOLE_Proton = 0;

	private const sbyte HOLE_Ak = 1;

	private const sbyte HOLE_Small = 2;

	private const sbyte HOLE_Cannon = 3;

	private const sbyte HOLE_Rocket = 4;

	private const sbyte HOLE_Range = 5;

	private const sbyte HOLE_RANGCUA = 6;

	private const sbyte HOLE_GRENADE = 7;

	private const sbyte HOLE_Smallest = 8;

	private const sbyte HOLE_BigHole = 9;

	private Image map;

	public int[] aMap;

	private int[] aMapZoom;

	public static bool isDrawRGB = true;

	public static Image MANGNHEN;

	public static Image BULLRUA;

	private static sbyte curHoleType;

	public static Image[] holeIMask = new Image[10];

	public static Image[] holeIMask2 = new Image[10];

	public static int[][] holeAMask = new int[10][];

	private int holeW;

	private int holeH;

	public int width;

	public int height;

	public int x;

	public int y;

	public bool isDestroy;

	public int id;

	public static bool isTest = true;

	public static bool isA;

	public bool changed;

	public int index;

	public CMap()
	{
	}

	public CMap(int id, int x, int y, Image img, bool isDestroy)
	{
		this.x = x;
		this.y = y;
		this.id = id;
		this.isDestroy = isDestroy;
		if (img != null)
		{
			if (isDestroy)
			{
				createRGB(img);
			}
			else
			{
				map = img;
			}
		}
	}

	public static void getHole()
	{
		BULLRUA = CCanvas.loadImage2("/hole/bullrua.png");
		MANGNHEN = CCanvas.loadImage2("/hole/mangnhen.png");
		holeIMask[0] = CCanvas.loadImage2("/hole/h32x26.png");
		holeIMask[1] = CCanvas.loadImage2("/hole/smallhole.png");
		holeIMask[2] = CCanvas.loadImage2("/hole/smallhole.png");
		holeIMask[3] = CCanvas.loadImage2("/hole/h36x30.png");
		holeIMask[4] = CCanvas.loadImage2("/hole/rocket.png");
		holeIMask[5] = CCanvas.loadImage2("/hole/rangehole.png");
		holeIMask[6] = CCanvas.loadImage2("/hole/hrangcua.png");
		holeIMask[7] = CCanvas.loadImage2("/hole/hgrenade.png");
		holeIMask[8] = CCanvas.loadImage2("/hole/h14x12.png");
		holeIMask[9] = CCanvas.loadImage2("/hole/h55x50.png");
		holeIMask2[0] = CCanvas.loadImage2("/hole2/h32x26.png");
		holeIMask2[1] = CCanvas.loadImage2("/hole2/smallhole.png");
		holeIMask2[2] = CCanvas.loadImage2("/hole2/smallhole.png");
		holeIMask2[3] = CCanvas.loadImage2("/hole2/h36x30.png");
		holeIMask2[4] = CCanvas.loadImage2("/hole2/rocket.png");
		holeIMask2[5] = CCanvas.loadImage2("/hole2/rangehole.png");
		holeIMask2[6] = CCanvas.loadImage2("/hole2/hrangcua.png");
		holeIMask2[7] = CCanvas.loadImage2("/hole2/hgrenade.png");
		holeIMask2[8] = CCanvas.loadImage2("/hole2/h14x12.png");
		holeIMask2[9] = CCanvas.loadImage2("/hole2/h55x50.png");
	}

	public static sbyte getHoleType(int bulletType)
	{
		switch (bulletType)
		{
		case 0:
		case 32:
			return 3;
		case 1:
			return 1;
		case 2:
			return 0;
		case 6:
			return 6;
		case 7:
		case 31:
		case 37:
			return 8;
		case 3:
			return 9;
		case 9:
			return 5;
		case 10:
			return 4;
		case 11:
		case 58:
			return 2;
		case 12:
			return 6;
		case 15:
			return 7;
		case 22:
			return 7;
		case 24:
			return 3;
		case 25:
			return 8;
		case 19:
			return 2;
		case 20:
			return 0;
		case 27:
			return 1;
		case 17:
		case 18:
			return 2;
		case 30:
			return 0;
		case 21:
			return 2;
		case 42:
		case 43:
			return 7;
		case 44:
			return 2;
		case 45:
			return 7;
		case 47:
			return 8;
		case 48:
			return 3;
		case 52:
			return 3;
		case 57:
			return 7;
		default:
			return 0;
		}
	}

	public void createRGB(Image img)
	{
		isDrawRGB = mSystem.isDrawRGB;
		map = img;
		width = img.getRealImageWidth();
		height = img.getRealImageHeight();
		if (!MM.isExistId(id))
		{
			aMap = new int[width * height];
			img.getRGB(ref aMap, 0, width, 0, 0, width, height);
		}
		else
		{
			aMap = new int[width * height];
			int[] scr = MM.rgbMap(id);
			mSystem.arraycopy(scr, 0, aMap, 0, width * height);
		}
		if (mGraphics.zoomLevel == 1)
		{
			aMapZoom = aMap;
		}
		else if (!Main.isWindowsPhone)
		{
			aMapZoom = Hqx.HqxZoom(mGraphics.zoomLevel, aMap, width, height);
			map = Image.createRGBImage(aMapZoom, width * mGraphics.zoomLevel, height * mGraphics.zoomLevel, true);
		}
		if (!isDrawRGB)
		{
			if (!isA)
			{
				isTest = true;
				isA = true;
			}
			aMapZoom = null;
		}
	}

	public void paint(mGraphics g)
	{
		if (map != null)
		{
			if (!Main.isWindowsPhone)
			{
				g.drawImage(map, x, y, 0, false);
			}
			else
			{
				g.drawImageScale(map, x, y, width, height, 0, false);
			}
		}
	}

	public void update()
	{
		if (changed)
		{
			if (mGraphics.zoomLevel == 1)
			{
				map = Image.createRGBImage(aMap, width, height, true);
			}
			else if (!Main.isWindowsPhone)
			{
				aMapZoom = Hqx.HqxZoom(mGraphics.zoomLevel, aMap, width, height);
				map = Image.createRGBImage(aMapZoom, width * mGraphics.zoomLevel, height * mGraphics.zoomLevel, true);
			}
			else
			{
				map = Image.createRGBImage(aMap, width, height, true);
			}
			aMapZoom = null;
		}
		changed = false;
	}

	public void makeHole(int bx, int by, int bullType)
	{
		CRes.outz("bx= " + bx + " by=" + by);
		curHoleType = getHoleType(bullType);
		holeW = holeIMask[curHoleType].getRealImageWidth();
		holeH = holeIMask[curHoleType].getRealImageHeight();
		if (holeAMask[curHoleType] == null)
		{
			holeAMask[curHoleType] = new int[holeW * holeH];
			holeIMask[curHoleType].getRGB(ref holeAMask[curHoleType], 0, holeW, 0, 0, holeW, holeH);
		}
		int num = x;
		int num2 = y;
		int num3 = bx - holeW / 2;
		int num4 = by - holeH / 2;
		int num5 = 0;
		int num6 = 0;
		int num7 = 0;
		int num8 = 0;
		if (num3 < num)
		{
			num5 = num - num3;
			num6 = holeW;
			if (num6 - num5 > width)
			{
				num6 = num5 + width;
			}
		}
		else
		{
			num5 = 0;
			num6 = num5 + (width - num3 + num);
			if (num6 > holeW)
			{
				num6 = holeW;
			}
		}
		if (num4 < num2)
		{
			num7 = num2 - num4;
			num8 = holeH;
			if (num8 - num7 > height)
			{
				num8 = num7 + height;
			}
		}
		else
		{
			num7 = 0;
			num8 = num7 + (height - num4 + num2);
			if (num8 > holeH)
			{
				num8 = holeH;
			}
		}
		int num9 = 0;
		int num10 = 0;
		num9 = bx - x - holeW / 2;
		num10 = by - y - holeH / 2;
		bool flag = false;
		try
		{
			for (int i = num7; i < num8; i++)
			{
				for (int j = num5; j < num6; j++)
				{
					if (CRes.inRect(num9 + j, num10 + i, 0, 0, width, height))
					{
						if (holeAMask[curHoleType][i * holeW + j] == 16711680 && CRes.isLand(getPixel(num9 + j, num10 + i)))
						{
							aMap[(num10 + i) * width + num9 + j] = 0;
						}
						else if (holeAMask[curHoleType][i * holeW + j] == 0)
						{
							aMap[(num10 + i) * width + num9 + j] = mSystem.transColor;
						}
					}
				}
			}
		}
		catch (Exception ex)
		{
			Cout.LogTryCatch("try:  " + ex.StackTrace.ToString());
		}
	}

	public void makeHoleForHD(int bx, int by, int bullType)
	{
		changed = true;
	}

	public static int getHoleW(int bulletType)
	{
		return holeIMask[getHoleType(bulletType)].getRealImageWidth();
	}

	public static int getHoleH(int bulletType)
	{
		return holeIMask[getHoleType(bulletType)].getRealImageHeight();
	}

	public static int getHoleW2(int bulletType)
	{
		return holeIMask2[getHoleType(bulletType)].getRealImageWidth();
	}

	public static int getHoleH2(int bulletType)
	{
		return holeIMask2[getHoleType(bulletType)].getRealImageHeight();
	}

	public int getPixel(int x, int y)
	{
		return aMap[y * width + x];
	}

	public static void initHoleMask()
	{
	}
}
