public override string ToString() { StringBuilder builder = new StringBuilder(); builder.AppendLine(string.Format("{0}: [{1} - {2}, {3} - {4}] ({5}, {6})", this.GetType().Name, MinX, MaxX, MinY, MaxY, Width, Height)); builder.AppendLine("{"); for (int y = MinY; y < MaxY; y++) { for (int x = MinX; x < MaxX; x++) { BefungeCommand bc = this[x, y]; builder.Append(bc.GetCommandCode()); } builder.AppendLine(); } builder.AppendLine("}"); List <TagLocation> tags = FindAllTags(); foreach (TagLocation tag in tags) { builder.AppendFormat("[ ({0:000}|{1:000}):'{2}' := {3} ]{4}", tag.X, tag.Y, tag.Command.GetCommandCode(), tag.Tag, Environment.NewLine); } return(builder.ToString()); }
// x1, y1 included -- x2, y2 excluded public void Fill(int x1, int y1, int x2, int y2, BefungeCommand c, CodeTag topleftTag = null, bool skipExactCopies = false) { if (x1 >= x2) { return; } if (y1 >= y2) { return; } for (int x = x1; x < x2; x++) { for (int y = y1; y < y2; y++) { if (skipExactCopies && c.EqualsTagLess(this[x, y])) { // Do nothing - skiped } else if (x == x1 && y == y1 && topleftTag != null) { this[x, y] = c.CopyWithTag(topleftTag); } else { this[x, y] = c; } } } }
public void ForceNonEmpty(BefungeCommand cmd) { if (Size == 0) { this[0, 0] = cmd; } }
public void AppendLeft(int row, BefungeCommand c) { CodePiece p = new CodePiece(); p[0, row] = c; AppendLeft(p); }
public void AppendTop(int col, BefungeCommand c) { CodePiece p = new CodePiece(); p[col, 0] = c; AppendTop(p); }
public static CodePiece Repeat(int count, BefungeCommand cmd) { CodePiece p = new CodePiece(); for (int i = 0; i < count; i++) { p[i, 0] = cmd; } return(p); }
public void Append(BefungeCommand c, bool reverse) { if (reverse) { AppendLeft(c); } else { AppendRight(c); } }
public static CodePiece DoCompressVertically(CodePiece t, CodePiece b) { if (t.Width == 0 || b.Width == 0) { return(null); } CodePiece connect = new CodePiece(); int yT = t.MaxY - 1; int yB = b.MinY; for (int x = Math.Min(t.MinX, b.MinX); x < Math.Max(t.MaxX, b.MaxX); x++) { CodeTag tag = null; if (t[x, yT].Tag != null && b[x, yB].Tag != null) { return(null); // Can't compress - two tags would need to be merged } tag = t[x, yT].Tag ?? b[x, yB].Tag; if (t[x, yT].Type == BefungeCommandType.NOP && b[x, yB].Type == BefungeCommandType.NOP) { connect[x, 0] = new BefungeCommand(BefungeCommandType.NOP, tag); } else if (t[x, yT].Type != BefungeCommandType.NOP && b[x, yB].Type != BefungeCommandType.NOP) { if (t[x, yT].Type == b[x, yB].Type && t[x, yT].Param == b[x, yB].Param && t[x, yT].IsCompressable()) { connect[x, 0] = new BefungeCommand(t[x, yT].Type, t[x, yT].Param, tag); } else { return(null); // Can't compress - two commands are colliding } } else if (t[x, yT].Type != BefungeCommandType.NOP) { connect[x, 0] = new BefungeCommand(t[x, yT].Type, t[x, yT].Param, tag); } else if (b[x, yB].Type != BefungeCommandType.NOP) { connect[x, 0] = new BefungeCommand(b[x, yB].Type, b[x, yB].Param, tag); } else { throw new WTFException(); } } return(connect); }
public void ReplaceWalkway(int x, int y, BefungeCommand cmd, bool deleteTags) { if (this[x, y].EqualsTagLess(BCHelper.Walkway) || (deleteTags && this[x, y].Type == BefungeCommandType.Walkway)) { ForceSet(x, y, cmd); } else { this[x, y] = cmd; } }
public static CodePiece DoCompressHorizontally(CodePiece l, CodePiece r) { if (l.Width == 0 || r.Width == 0) { return(null); } CodePiece connect = new CodePiece(); int xL = l.MaxX - 1; int xR = r.MinX; for (int y = Math.Min(l.MinY, r.MinY); y < Math.Max(l.MaxY, r.MaxY); y++) { CodeTag tag = null; if (l[xL, y].Tag != null && r[xR, y].Tag != null) { return(null); // Can't compress - two tags would need to be merged } tag = l[xL, y].Tag ?? r[xR, y].Tag; if (l[xL, y].Type == BefungeCommandType.NOP && r[xR, y].Type == BefungeCommandType.NOP) { connect[0, y] = new BefungeCommand(BefungeCommandType.NOP, tag); } else if (l[xL, y].Type != BefungeCommandType.NOP && r[xR, y].Type != BefungeCommandType.NOP) { if (l[xL, y].Type == r[xR, y].Type && l[xL, y].Param == r[xR, y].Param && l[xL, y].IsCompressable()) { connect[0, y] = new BefungeCommand(l[xL, y].Type, l[xL, y].Param, tag); } else { return(null); // Can't compress - two commands are colliding } } else if (l[xL, y].Type != BefungeCommandType.NOP) { connect[0, y] = new BefungeCommand(l[xL, y].Type, l[xL, y].Param, tag); } else if (r[xR, y].Type != BefungeCommandType.NOP) { connect[0, y] = new BefungeCommand(r[xR, y].Type, r[xR, y].Param, tag); } else { throw new WTFException(); } } return(connect); }
public void SetTag(int x, int y, CodeTag tag, bool force = false) { BefungeCommand cmd = this[x, y]; if (cmd.HasTag() && !force) { throw new InvalidCodeManipulationException("Tryed to remove existing Tag: " + cmd.Tag + " with: " + tag); } BefungeCommand newcmd = new BefungeCommand(cmd.Type, cmd.Param, tag); ForceSet(x, y, newcmd); }
public string ToSimpleString() { StringBuilder builder = new StringBuilder(); for (int y = MinY; y < MaxY; y++) { for (int x = MinX; x < MaxX; x++) { BefungeCommand bc = this[x, y]; builder.Append(bc.GetCommandCode()); } if (y < MaxY - 1) { builder.AppendLine(); } } return(builder.ToString()); }
private void ForceSet(int x, int y, BefungeCommand value) // Suppresses CodeModificationException { if (!IsIncluded(x, y)) { Expand(x, y); } BefungeCommand prev = commandArr[x - MinX][y - MinY]; if (prev.HasTag()) { tagCache.Remove(prev.Tag); } commandArr[x - MinX][y - MinY] = value; if (value.HasTag()) { tagCache.Add(value.Tag); } }
private void Set(int x, int y, BefungeCommand value) { if (!IsIncluded(x, y)) { Expand(x, y); } if (commandArr[x - MinX][y - MinY].Type != BefungeCommandType.NOP) { throw new InvalidCodeManipulationException("Modification of CodePiece : " + x + "|" + y); } if (HasTag(value.Tag)) { throw new InvalidCodeManipulationException(string.Format("Duplicate Tag in CodePiece : [{0},{1}] = '{2}' = [{3},{4}])", x, y, value.Tag.ToString(), FindTag(value.Tag).X, FindTag(value.Tag).Y)); } commandArr[x - MinX][y - MinY] = value; if (value.HasTag()) { tagCache.Add(value.Tag); } }
public bool EqualsTagLess(BefungeCommand c) { return(!HasTag() && !c.HasTag() && this.Type == c.Type && this.Param == c.Param); }
public void AppendTop(BefungeCommand c) { AppendTop(0, c); }
public void AppendBottom(BefungeCommand c) { AppendBottom(0, c); }
public void AppendLeft(BefungeCommand c) { AppendLeft(0, c); }
public void AppendRight(BefungeCommand c) { AppendRight(0, c); }
public CodePiece(BefungeCommand cmd) : this() { this[0, 0] = cmd; }