aboutsummaryrefslogtreecommitdiff
path: root/source/app.d
diff options
context:
space:
mode:
authorHolden Rohrer <hr@hrhr.dev>2021-08-31 18:36:27 -0400
committerHolden Rohrer <hr@hrhr.dev>2021-08-31 18:36:27 -0400
commit9b80e6eef31b0c852c772ba6552e419856fe0aa9 (patch)
tree2f3e077a8b6c6f8351bcd0d7f18e2f04a9da7af0 /source/app.d
parentec9ce614d87a47137cfbbb0f6d13aa30e4a1239c (diff)
finished in-world portal features
Diffstat (limited to 'source/app.d')
-rw-r--r--source/app.d132
1 files changed, 77 insertions, 55 deletions
diff --git a/source/app.d b/source/app.d
index 1d04fc9..7faaa75 100644
--- a/source/app.d
+++ b/source/app.d
@@ -2,12 +2,12 @@ import deimos.ncurses;
import std.string : toStringz;
import core.stdc.locale;
import std.format;
+import std.stdio;
import board;
import spots;
-import std.stdio;
+import portals;
-void main()
-{
+void main() {
setlocale(LC_CTYPE, ""); // ncurses locales are weird
auto stdscr = initscr();
@@ -20,19 +20,23 @@ void main()
scrollok(stdscr, true); // allows scrolling in up/down arrows
nonl(); // "return" goes through input
- auto overlay = new OverlaySource(new RandomSource());
- auto board = new Board(overlay);
+ 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 = "0";
+ disp.status = "Press [space] to pick up and set down rocks";
refresh();
- outer: while (true)
- {
+ import std.bigint : BigInt;
+
+ BigInt[][int] portal;
+ int lastPortalId;
+
+ outer: while (true) {
auto c = getch();
- switch (c)
- {
+ switch (c) {
case ',':
case KEY_UP:
disp.up();
@@ -53,16 +57,13 @@ void main()
disp.getdims();
break;
case ' ':
- if (overlay[disp.y, disp.x].isRock())
- {
- overlay[disp.y, disp.x] = Spot(' ');
+ 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)
- {
- overlay[disp.y, disp.x] = Spot('*');
+ } else if (rocks > 0 && source[disp.y, disp.x].isSpace()) {
+ source[disp.y, disp.x] = Spot('*');
rocks--;
disp.status = format("%d", rocks);
disp.print();
@@ -72,38 +73,60 @@ void main()
break outer;
case 's':
auto filename = stdscr.readquery("Save:");
- try
- {
- auto file = File(filename, "wb");
- file.writefln!"%s %s"(disp.y, disp.x);
- file.writefln!"%d"(rocks);
- overlay.save(file);
- disp.print();
- }
- catch (Exception e)
- {
+ 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) {
+ }
}
break;
case 'l':
auto filename = stdscr.readquery("Load:");
- try
- {
- import std.algorithm.iteration : splitter, map;
- import std.conv : to;
- import std.bigint;
+ 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);
- overlay.load(file);
+ 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) {
+ }
}
- catch (Exception e)
- {
- printw("exception!");
+ 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;
@@ -113,8 +136,9 @@ void main()
}
}
-string readquery(WINDOW* stdscr, string query)
-{
+immutable char ESC = 27;
+
+string readquery(WINDOW* stdscr, string query) {
import std.range : replicate;
import std.conv : to;
import std.array : appender;
@@ -127,31 +151,29 @@ string readquery(WINDOW* stdscr, string query)
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())
- {
+ while (auto ch = getch()) {
if (ch == '\r' || ch == EOF)
break;
- if (ch == '\b' || ch == KEY_BACKSPACE || ch == 127)
- {
- if (build[].length > 0)
- {
+ 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
- {
+ } else if (ch == ESC) {
+ return null;
+ } else {
build ~= cast(char) ch;
addch(ch);
p++;
}
}
- scrollok(stdscr, true);
return cast(string) build[];
}