9 // Originally, this was pragma(inline, true) pure final void pline(T...)(string s, T args)
10 // But the opportunity was too good to pass up
12 //pragma(inline, true) pure const static inout shared ref nothrow override @property @nogc @safe public final void
13 // in all its glory. But after about a year, I found out that this didn't actually work, which brings you to the state you see today :<<
14 pragma(inline
, true) public final void pline(T
...)(T args
) {
16 import std
.string
: format
;
17 if (args
.length
== 1) {
18 this.pline(to
!string(args
[0]));
20 this.pline(format(args
));
30 private struct startendspec
{ int startx
, endx
, cursx
, starty
, endy
, cursy
; }
32 // Find the x-y coordinates to start drawing from in the map so the camera is centred on the player
33 // FUN FACT while coding this, I had no idea what it did or how
34 // it worked. I still have no idea how it works. But it does!
35 private @nogc @safe final startendspec
focuscamera(int width
, int height
, int x
, int y
) {
36 import std
.math
: round
;
38 int startx
, starty
, endx
, endy
;
39 int offsetx
, offsety
; // offsets are distance from top edge, left edge to centre
43 offsetx
= cast(int)round(width
/ 2.0);
44 offsety
= cast(int)round(height
/ 2.0);
46 // Don't keep the map centred when we're near an edge,
47 // actually move the @ closer to the edge
66 endx
= startx
+ width
;
67 endy
= starty
+ height
;
74 cursx = width - (map_x - x) - 1;
79 cursy = height - (map_y - y) - 1;
93 class Default_chargfx(G0type
: CharGfx0
): Graphics1
{
97 this(Game g
, string title
="") {
99 this.graphics0
= new G0type();
101 graphics0
.settitle(title
);
103 void close() { graphics0
.close(); }
106 auto lens
= focuscamera(maxx(), maxy(), game
.u
.loc
.x
, game
.u
.loc
.y
);
107 uint tmpy
, tmpx
; // position of character on the screen
111 foreach (int i
; lens
.starty
..lens
.endy
) {
113 foreach (int j
; lens
.startx
..lens
.endx
) {
115 Vector2n maploc
= Vector2n(i
, j
);
117 if ((mon
= game
.mon_at(maploc
)) is null) {
120 if (!game
.map
[maploc
].visible
) {
124 /* x and y both start at 1, so we want to get them to 0
125 * in order to align them with the edges of the terminal.
126 * But it's okay to "add" 1 to y, because we want to leave
127 * an extra line up to for messages.
130 graphics0
.mvaddch(mon
.glyph
, tmpy
, tmpx
-1, mon
.fgcolour
, mon
.bgcolour
, bold
, italic
, underline
, reverse
);
138 auto lens
= focuscamera(maxx(), maxy(), game
.u
.loc
.x
, game
.u
.loc
.y
);
140 foreach (int i
; lens
.starty
..lens
.endy
) {
142 foreach (int j
; lens
.startx
..lens
.endx
) {
145 Vector2n maploc
= Vector2n(i
, j
);
147 /* x and y both start at 1, so we want to get them to 0
148 * in order to align them with the edges of the terminal.
149 * But it's okay to "add" 1 to y, because we want to leave
150 * an extra line up to for messages.
152 with (game
.map
[maploc
].attrs
)
153 graphics0
.mvaddch(game
.map
[maploc
].glyph
, tmpy
, tmpx
-1, game
.map
[maploc
].visible ? game
.map
[maploc
].fgcolour
.lighten(20) : game
.map
[maploc
].fgcolour
.darken(20), game
.map
[maploc
].bgcolour
, bold
, italic
, underline
, reverse
);
159 void pline(string msg
) {
160 if (msg
.length
== 0) {
164 // TODO chop stuff up based on words, not character counts
165 pure string
[] chopupmsg(string msgtext
, int x
) {
168 immutable uint maxlen
= x
- cast(int)" --More--".length
;
169 while (msgtext
.length
> x
) {
170 buf
~= (msgtext
[tmp
..tmp
+maxlen
] ~ " --More--");
172 msgtext
= msgtext
[tmp
..$];
178 graphics0
.printext(fillstr(maxx()), 0, 0);
181 if (msg
.length
<= maxx()) {
183 graphics0
.printext(msg
, 0, 0);
186 string
[] buffer
= chopupmsg(msg
, /*cast(int)*/maxx());
188 loop: foreach (lineindex
; 0..buffer
.length
) {
189 graphics0
.printext(buffer
[lineindex
], 0, 0);
192 c
= graphics0
.getch();
195 graphics0
.printext(buffer
[$-1], 0, 0);
198 if (lineindex
< buffer
.length
-1) {
199 while ((c
!= '\n') && (c
!= ' ')) {
202 graphics0
.printext(buffer
[$-1], 0, 0);
213 int maxx() { return graphics0
.maxx(); }
214 int maxy() { return graphics0
.maxy(); }
221 dchar getch() { return graphics0
.getch
; }