From f7295648522de31ddaa6d68c54242ab527da0311 Mon Sep 17 00:00:00 2001 From: Elronnd Date: Sat, 11 Feb 2017 15:44:59 -0700 Subject: [PATCH] Re-enable the walkability checks, allow colours of the form #xxx/xxx, not just #xxxxxx/xxxxxx, mark the rgbcolour2xtermcolour as pure and make it take up less ram --- src/game.d | 6 +++--- src/util.d | 41 ++++++++++++++++++++++++++--------------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/game.d b/src/game.d index 99848b4..9fbd2dd 100644 --- a/src/game.d +++ b/src/game.d @@ -130,9 +130,9 @@ class Game { if (map[tmpy][tmpx].walkable) { map[tmpy][tmpx].fgcolour = RGBColour("#4c4c4c"); } else { - map[tmpy][tmpx].fgcolour = RGBColour("#eeeeee"); + map[tmpy][tmpx].fgcolour = RGBColour("#eee"); } - map[tmpy][tmpx].bgcolour = RGBColour("#000000"); //cast(ubyte)rnd(1, 255); + map[tmpy][tmpx].bgcolour = RGBColour("#000"); if ((tmpy == 0) || (tmpx == 0) || (tmpy == map_y+2) || (tmpx == map_x+2)) map[tmpy][tmpx].walkable = false; @@ -177,7 +177,7 @@ class Game { int deltax, deltay; refresh(); void handlemove(int yshift, int xshift, Being b) { - if (/*map[b.y+yshift][b.x+xshift].walkable*/true) { + if (map[b.y+yshift][b.x+xshift].walkable) { map[b.y][b.x].being = null; map[b.y][b.x].setdefglyph(); y += yshift; diff --git a/src/util.d b/src/util.d index 31d5ce5..d1b1524 100644 --- a/src/util.d +++ b/src/util.d @@ -19,7 +19,7 @@ class InternalError: DException { bool isvalidcolour(string hexcolour) { import std.regex: ctRegex, matchFirst; - auto hexcolourmatch = ctRegex!("#?[0-9a-fA-F]{6}"); + auto hexcolourmatch = ctRegex!("#?[0-9a-fA-F]{3,6}"); if (matchFirst(hexcolour, hexcolourmatch).empty) { return false; @@ -41,14 +41,22 @@ struct RGBColour { pure this(string hexcolour, bool hasvalidated=false) { import std.conv: to; - if (hexcolour.length == 7) { + // strip leading #, if present + if ((hexcolour.length == 7) || (hexcolour.length == 4)) { hexcolour = hexcolour[1..$]; } if (hasvalidated) { - r = hexcolour[0..2].to!ubyte(16); - g = hexcolour[2..4].to!ubyte(16); - b = hexcolour[4..6].to!ubyte(16); + if (hexcolour.length == 6) { + r = hexcolour[0..2].to!ubyte(16); + g = hexcolour[2..4].to!ubyte(16); + b = hexcolour[4..6].to!ubyte(16); + } else { + // The ranges here are to get a string just one character long so to!ubyte works + r = cast(ubyte)(hexcolour[0..1].to!ubyte(16)*16); + g = cast(ubyte)(hexcolour[1..2].to!ubyte(16)*16); + b = cast(ubyte)(hexcolour[2..3].to!ubyte(16)*16); + } } else { throw new InternalError("RGBColour constructor called but rgbcolour not validated!"); } @@ -90,16 +98,19 @@ pure string fillstr(int length, char ch=' ') { /* It's supposed to be incredibly performant...but honestly I used it because * I could copy-paste it easily. */ -ubyte get256colour(RGBColour colour) { - enum int[6] i2cv = [0, 0x5f, 0x87, 0xaf, 0xd7, 0xff]; - pragma(inline, true) auto v2ci = (int v) => v < 48 ? 0 : v < 115 ? 1 : (v - 35) / 40; - pragma(inline, true) auto colour_index = (int r, int g, int b) => 36*r + 6*g + b; - pragma(inline, true) auto distance = (int r1, int g1, int b1, int r2, int g2, int b2) => (r1-r2)*(r1-r2) + (g1-g2)*(g1-g2) + (b1-b2)*(b1-b2); - int ir = v2ci(colour.r), ig = v2ci(colour.g), ib = v2ci(colour.b); - int average = (colour.r + colour.g + colour.b) / 3; - int igray = average > 238 ? 23 : (average-3) / 10; - int cr = i2cv[ir], cg = i2cv[ig], cb = i2cv[ib]; - int gv = cast(ubyte)(8 + 10 * igray); +pure ubyte get256colour(RGBColour colour) { + enum ubyte[6] i2cv = [0, 0x5f, 0x87, 0xaf, 0xd7, 0xff]; + pragma(inline, true) { + pure ubyte function(ubyte) v2ci = cast(ubyte function(ubyte) pure)(ubyte v) => v < 48 ? 0 : v < 115 ? 1 : (v - 35) / 40; + auto colour_index = (int r, int g, int b) => 36*r + 6*g + b; + auto distance = (int r1, int g1, int b1, int r2, int g2, int b2) => (r1-r2)*(r1-r2) + (g1-g2)*(g1-g2) + (b1-b2)*(b1-b2); + } + + ubyte ir = v2ci(colour.r), ig = v2ci(colour.g), ib = v2ci(colour.b); + ubyte average = (colour.r + colour.g + colour.b) / 3; + ubyte igray = average > 238 ? 23 : (average-3) / 10; + ubyte cr = i2cv[ir], cg = i2cv[ig], cb = i2cv[ib]; + ubyte gv = cast(ubyte)(8 + 10 * igray); int colour_err = distance(cr, cg, cb, colour.r, colour.g, colour.b); int gray_err = distance(gv, gv, gv, colour.r, colour.g, colour.b); -- 2.11.4.GIT