aboutsummaryrefslogtreecommitdiff
path: root/source/app.d
blob: ec4601239bfaab3e37b206e6ad23334716539271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import deimos.ncurses;
import std.string : toStringz;
import core.stdc.locale;
import std.format;
import std.stdio;
import board;
import spots;
import portals;

void main() {
    setlocale(LC_CTYPE, ""); // ncurses locales are weird

    auto stdscr = initscr();
    scope (exit)
        endwin();
    cbreak(); // disables line buffering for input
    noecho(); // input doesn't display on-screen
    intrflush(stdscr, true);
    keypad(stdscr, true); // allows arrow keys to work
    scrollok(stdscr, true); // allows scrolling in up/down arrows
    nonl(); // "return" goes through input

    auto source = new OverlaySource(new RandomSource());
    auto portals = new Portals(source);
    auto board = new Board(source);
    auto disp = new BoardDisplay(board, stdscr);
    disp.print();
    int rocks = 0;
    disp.status = "Press [space] to pick up and set down rocks";
    refresh();

    import std.bigint : BigInt;

    BigInt[][int] portal;
    int lastPortalId;

    outer: while (true) {
        auto c = getch();
        switch (c) {
        case ',':
        case KEY_UP:
            disp.up();
            break;
        case 'o':
        case KEY_DOWN:
            disp.down();
            break;
        case 'e':
        case KEY_RIGHT:
            disp.right();
            break;
        case 'a':
        case KEY_LEFT:
            disp.left();
            break;
        case KEY_RESIZE:
            disp.getdims();
            break;
        case ' ':
            if (source[disp.y, disp.x].isRock()) {
                source[disp.y, disp.x] = Spot(' ');
                rocks++;
                disp.status = format("%d", rocks);
                disp.print();
            } else if (rocks > 0 && source[disp.y, disp.x].isSpace()) {
                source[disp.y, disp.x] = Spot('*');
                rocks--;
                disp.status = format("%d", rocks);
                disp.print();
            }
            break;
        case 'q':
            break outer;
        case 's':
            auto filename = stdscr.readquery("Save:");
            if (filename) {
                try {
                    auto file = File(filename, "wb");
                    file.writefln!"%s %s"(disp.y, disp.x);
                    file.writefln!"%d"(rocks);
                    source.save(file);
                    portals.save(file);
                } catch (Exception e) {
                }
            }
            disp.print();
            break;
        case 'l':
            auto filename = stdscr.readquery("Load:");
            if (filename) {
                try {
                    import std.algorithm.iteration : splitter, map;
                    import std.conv : to;

                    auto file = File(filename);
                    auto coord = file.readln.splitter().map!(to!BigInt);
                    disp.y = coord.front;
                    coord.popFront;
                    disp.x = coord.front;
                    file.readf!"%d\n"(rocks);
                    disp.status = format("%d", rocks);
                    source.load(file);
                    portals.load(file);
                } catch (Exception e) {
                }
            }
            disp.print();
            break;
        case '/':
            auto sp = source[disp.y, disp.x];
            if (sp.isSpace) {
                if (rocks >= 10) {
                    rocks -= 10;
                    portals.add(disp.y, disp.x);
                }
            } else if (sp.isPortal) {
                rocks += 10;
                portals.remove(disp.y, disp.x);
            }
            disp.status = format("%d", rocks);
            disp.print();
            break;
        case 'g':
            auto sp = source[disp.y, disp.x];
            if (sp.isPortal) {
                auto dest = portals.get(sp.id).pair;
                if (dest != null) {
                    disp.y = dest.y;
                    disp.x = dest.x;
                }
            }
            disp.print();
            break;
        default:
            continue;
        }
    }
}

immutable char ESC = 27;

string readquery(WINDOW* stdscr, string query) {
    import std.range : replicate;
    import std.conv : to;
    import std.array : appender;

    int height;
    int width;
    getmaxyx(stdscr, height, width);

    mvprintw(height, 0, toStringz(query ~ " ".replicate(width - query.length)));
    move(height, cast(int) query.length + 1);

    scrollok(stdscr, false);
    scope (success)
        scrollok(stdscr, true);
    auto build = appender!(char[]);

    int p = cast(int) query.length + 1;
    while (auto ch = getch()) {
        if (ch == '\r' || ch == EOF)
            break;
        if (ch == '\b' || ch == KEY_BACKSPACE || ch == 127) {
            if (build[].length > 0) {
                p--;
                mvaddch(height, p, ' ');
                build.shrinkTo(build[].length - 1);
                move(height, p);
            }
        } else if (ch == ESC) {
            return null;
        } else {
            build ~= cast(char) ch;
            addch(ch);
            p++;
        }
    }

    return cast(string) build[];
}