예제 #1
0
	private RefillFlowRecord DoApply(Container2D_Rectangular container, FillInfo[,] fillInfoMap)
	{
		var ret = new RefillFlowRecord();
		var slots = container.WrapperRect;

		container.ForeachSlot((x, y, slot)=>{
			if (null != slot) return;
			var fi = fillInfoMap[y, x];
			if (fi.IsOnSpot){
				ret.OnSpotList.Add(new Pos2D(x, y));
			}
		});
		var ends = CollectRefillEnds(container, fillInfoMap);
		var dir = new Dictionary<int, Tuple<SlotWrapper2D, int>>();
		foreach (var e in ends)
		{
			var pos = fillInfoMap[e.y, e.x].ancestorPos;
			int inverseDepth = 0;
			while (container.IsLegalPosition(pos.x, pos.y))
			{
				var s = slots[pos.y, pos.x];
				if (null != s)
				{
					inverseDepth++;
					if (!dir.ContainsKey(s.Trait))
					{
						dir.Add(s.Trait, new Tuple<SlotWrapper2D, int>(s, inverseDepth));
					}
					else if (dir[s.Trait].item2 < inverseDepth)
					{
						dir[s.Trait].item2 = inverseDepth;
					}
				}
				pos = fillInfoMap[pos.y, pos.x].ancestorPos;
			}
		}
		var list = new List<Tuple<SlotWrapper2D, int>>();
		foreach (var kvp in dir)
		{
			list.Add(kvp.Value);
		}
		list.Sort((lhr, rhr)=>{
			return (lhr.item2 < rhr.item2) ? -1 : 1;
		});

		foreach (var t in list)
		{
			var src = t.item1.pos;
			var moveTo = DepthFirstSearch(slots, fillInfoMap, src.x, src.y);
			var dst = moveTo.item1;

			var path = new RefillFlowRecord.Path();
			path.src = src.Clone();
			path.dst = dst.Clone();

			container.SwapSlot(src.x, src.y, dst.x, dst.y);
			var cur = path.dst;
			do {
				path.movements.Add(cur);
				cur = fillInfoMap[cur.y, cur.x].ancestorPos;
			} while (cur != path.src);

			path.movements.Add(path.src);
			path.movements.Reverse();

			ret.NonFillMovements.Add(path);
		}

		ret.FillMovements = CollectFillPathList(container, fillInfoMap);

		return ret;
	}
예제 #2
0
    private RefillFlowRecord DoApply(Container2D_Rectangular container, FillInfo[,] fillInfoMap)
    {
        var ret   = new RefillFlowRecord();
        var slots = container.WrapperRect;

        container.ForeachSlot((x, y, slot) => {
            if (null != slot)
            {
                return;
            }
            var fi = fillInfoMap[y, x];
            if (fi.IsOnSpot)
            {
                ret.OnSpotList.Add(new Pos2D(x, y));
            }
        });
        var ends = CollectRefillEnds(container, fillInfoMap);
        var dir  = new Dictionary <int, Tuple <SlotWrapper2D, int> >();

        foreach (var e in ends)
        {
            var pos          = fillInfoMap[e.y, e.x].ancestorPos;
            int inverseDepth = 0;
            while (container.IsLegalPosition(pos.x, pos.y))
            {
                var s = slots[pos.y, pos.x];
                if (null != s)
                {
                    inverseDepth++;
                    if (!dir.ContainsKey(s.Trait))
                    {
                        dir.Add(s.Trait, new Tuple <SlotWrapper2D, int>(s, inverseDepth));
                    }
                    else if (dir[s.Trait].item2 < inverseDepth)
                    {
                        dir[s.Trait].item2 = inverseDepth;
                    }
                }
                pos = fillInfoMap[pos.y, pos.x].ancestorPos;
            }
        }
        var list = new List <Tuple <SlotWrapper2D, int> >();

        foreach (var kvp in dir)
        {
            list.Add(kvp.Value);
        }
        list.Sort((lhr, rhr) => {
            return((lhr.item2 < rhr.item2) ? -1 : 1);
        });

        foreach (var t in list)
        {
            var src    = t.item1.pos;
            var moveTo = DepthFirstSearch(slots, fillInfoMap, src.x, src.y);
            var dst    = moveTo.item1;

            var path = new RefillFlowRecord.Path();
            path.src = src.Clone();
            path.dst = dst.Clone();

            container.SwapSlot(src.x, src.y, dst.x, dst.y);
            var cur = path.dst;
            do
            {
                path.movements.Add(cur);
                cur = fillInfoMap[cur.y, cur.x].ancestorPos;
            } while (cur != path.src);

            path.movements.Add(path.src);
            path.movements.Reverse();

            ret.NonFillMovements.Add(path);
        }

        ret.FillMovements = CollectFillPathList(container, fillInfoMap);

        return(ret);
    }