From a224088372c273460a3ae5786a787637915693a3 Mon Sep 17 00:00:00 2001 From: Jay Robson Date: Sun, 14 Jan 2024 18:35:12 +1100 Subject: [PATCH] added controls --- src/main.cpp | 34 +++++++++++++++++- src/reactor/control/control_rod.cpp | 8 +++++ src/reactor/control/control_rod.hpp | 3 ++ src/reactor/coolant/pipe.cpp | 2 +- src/reactor/reactor.hpp | 53 +++++++++++++++++++++++++++++ src/reactor/rod.hpp | 9 ++++- 6 files changed, 106 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 8a26f07..e1415bb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -43,16 +43,48 @@ int main() for(int y = 0; y < reactor.height; y++) { std::stringstream ss; - ss << *reactor.rods[x][y]; + sim::reactor::rod* r = reactor.rods[x][y]; + ss << *r; int px = X + (H - 1) * y; int py = Y + (W - 1) * x; display::draw_text(px + 1, py + 2, ss.str().c_str()); display::draw_box(px, py, H, W); + + if(r->should_select() && x == reactor.cursor_x && y == reactor.cursor_y) + { + display::draw_text(px + 1, py + W - 5, "[ ]"); + } + + if(r->is_selected()) + { + display::draw_text(px + 1, py + W - 4, "#"); + } } refresh(); + + int c = getch(); + + switch(c) + { + case KEY_LEFT: + reactor.move_cursor(-1); + break; + case KEY_RIGHT: + reactor.move_cursor(1); + break; + case KEY_UP: + reactor.update_selected(0.001); + break; + case KEY_DOWN: + reactor.update_selected(-0.001); + break; + case ' ': + reactor.toggle_selected(); + break; + } } return 0; diff --git a/src/reactor/control/control_rod.cpp b/src/reactor/control/control_rod.cpp index 3562009..c735756 100644 --- a/src/reactor/control/control_rod.cpp +++ b/src/reactor/control/control_rod.cpp @@ -47,3 +47,11 @@ void control_rod::update(double secs) absorbed += r_fast + r_slow; } +void control_rod::update_selected(double a) +{ + inserted += a; + + if(inserted > 1) inserted = 1; + if(inserted < 0) inserted = 0; +} + diff --git a/src/reactor/control/control_rod.hpp b/src/reactor/control/control_rod.hpp index 72f8d67..9a7c39d 100644 --- a/src/reactor/control/control_rod.hpp +++ b/src/reactor/control/control_rod.hpp @@ -24,6 +24,9 @@ public: virtual void update(double secs); void set_reactivity(double a); + + virtual bool should_select() const { return true; } + virtual void update_selected(double a); }; } diff --git a/src/reactor/coolant/pipe.cpp b/src/reactor/coolant/pipe.cpp index 49f5c55..0dd6385 100644 --- a/src/reactor/coolant/pipe.cpp +++ b/src/reactor/coolant/pipe.cpp @@ -22,7 +22,7 @@ void pipe::update(double secs) { update_rod(); - double v = vals[val_t::N_FAST] * 0.25; + double v = vals[val_t::N_FAST]; vals[val_t::N_FAST] -= v; vals[val_t::N_SLOW] += v; } diff --git a/src/reactor/reactor.hpp b/src/reactor/reactor.hpp index a5cafc0..d1854b0 100644 --- a/src/reactor/reactor.hpp +++ b/src/reactor/reactor.hpp @@ -16,6 +16,9 @@ struct reactor std::array, W> rods; + int cursor_x = 0; + int cursor_y = 0; + reactor(std::array rods) { for(int y = 0; y < H; y++) @@ -53,6 +56,56 @@ struct reactor rods[x][y]->update(secs); } } + + void update_selected(double v) + { + for(int y = 0; y < H; y++) + for(int x = 0; x < W; x++) + if(rods[x][y]->is_selected()) + { + rods[x][y]->update_selected(v); + } + } + + void move_cursor(int d) + { + for(;;) + { + cursor_x += d; + + while(cursor_x >= W) + { + cursor_x -= W; + cursor_y += 1; + } + + while(cursor_x < 0) + { + cursor_x += W; + cursor_y -= 1; + } + + cursor_y %= H; + + if(cursor_y < 0) + { + cursor_y += H; + } + + if(rods[cursor_x][cursor_y]->should_select()) + { + return; + } + } + } + + void toggle_selected() + { + if(rods[cursor_x][cursor_y]->should_select()) + { + rods[cursor_x][cursor_y]->toggle_selected(); + } + } }; } diff --git a/src/reactor/rod.hpp b/src/reactor/rod.hpp index 9da2df4..2590d3d 100644 --- a/src/reactor/rod.hpp +++ b/src/reactor/rod.hpp @@ -25,11 +25,17 @@ public: virtual double extract(val_t type, double k, double o); virtual double get(val_t type) const; + virtual bool should_select() const { return false; } + virtual void update_selected(double a) { } + + constexpr void toggle_selected() { selected = !selected; } + constexpr bool is_selected() { return selected; } + friend std::ostream& operator<<(std::ostream& o, const rod& r) { if(!r.should_display()) return o; - o << "Name: " << r.get_name() << "\n"; + o << r.get_name() << "\n"; r.display(o); o << "Heat: " << r.get(val_t::HEAT) << "\n"; o << "Fast: " << r.get(val_t::N_FAST) << "\n"; @@ -42,6 +48,7 @@ protected: double vals_in[VAL_N] = {0}; double vals[VAL_N] = {0}; + bool selected = false; virtual void display(std::ostream& o) const { }; virtual double get_k(val_t type) const { return 0; }