diff --git a/src/graphics/mesh/model.cpp b/src/graphics/mesh/model.cpp index 1735f56..067ed89 100644 --- a/src/graphics/mesh/model.cpp +++ b/src/graphics/mesh/model.cpp @@ -62,7 +62,7 @@ static unsigned int proc_texture(const proc_state& state, aiMaterial* mat, const static void proc_mesh(proc_state& state, glm::mat4 mat, aiMesh* mesh, const aiScene* scene) { aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex]; - unsigned int texid = proc_texture(state, material, scene); + unsigned int handle = proc_texture(state, material, scene); unsigned int offset = state.offset; for(unsigned int i = 0; i < mesh->mNumVertices; i++) @@ -71,7 +71,7 @@ static void proc_mesh(proc_state& state, glm::mat4 mat, aiMesh* mesh, const aiSc auto [x, y, z] = mesh->mVertices[i]; vertex.pos = glm::vec4(x, y, z, 1) * mat; - vertex.texid = texid; + vertex.texid = handle; if(mesh->HasNormals()) { diff --git a/src/parts.cpp b/src/parts.cpp index e8069fe..9860ed8 100644 --- a/src/parts.cpp +++ b/src/parts.cpp @@ -10,23 +10,26 @@ using namespace sim; reactor::coolant::vessel* parts::vessel; -reactor::reactor<5, 5>* parts::reactor; +reactor::reactor* parts::reactor; coolant::valve* parts::valve; coolant::pump* parts::pump; void parts::init() { + const char* layout[] = { + "#C#C#", + "CFCFC", + "#C#C#", + "CFCFC", + "#C#C#" + }; + vessel = new reactor::coolant::vessel(8, 10, 300, sim::coolant::WATER); - reactor = new reactor::reactor<5, 5>(sim::reactor::builder<5, 5>( + reactor = new sim::reactor::reactor(sim::reactor::builder(5, 5, reactor::fuel::fuel_rod(2000, 4000), reactor::control::control_rod(*vessel, 10000, 1), - reactor::coolant::pipe(*vessel), { - "#C#C#", - "CFCFC", - "#C#C#", - "CFCFC", - "#C#C#" - })); + reactor::coolant::pipe(*vessel), + layout)); valve = new coolant::valve(*vessel, 1, 500); pump = new coolant::pump(*vessel, 1e4, 15); diff --git a/src/parts.hpp b/src/parts.hpp index f52c5ad..674d341 100644 --- a/src/parts.hpp +++ b/src/parts.hpp @@ -10,7 +10,7 @@ namespace sim::parts { extern sim::reactor::coolant::vessel* vessel; -extern sim::reactor::reactor<5, 5>* reactor; +extern sim::reactor::reactor* reactor; extern sim::coolant::valve* valve; extern sim::coolant::pump* pump; diff --git a/src/reactor/builder.cpp b/src/reactor/builder.cpp new file mode 100644 index 0000000..58e9cf3 --- /dev/null +++ b/src/reactor/builder.cpp @@ -0,0 +1,43 @@ + +#include "builder.hpp" + +using namespace sim::reactor; + +sim::reactor::reactor sim::reactor::builder(const int W, const int H, fuel::fuel_rod fr, control::control_rod cr, coolant::pipe p, const char** lines) +{ + std::unique_ptr arr[W * H]; + + for(int y = 0; y < H; y++) + for(int x = 0; x < W; x++) + { + char c = lines[y][x]; + rod* r; + + switch(c) + { + case 'F': + r = new fuel::fuel_rod(fr); + break; + case 'C': + r = new control::control_rod(cr); + break; + case 'G': + r = new control::graphite_rod(); + break; + case 'H': + r = new coolant::heater(); + break; + case ' ': + r = new coolant::pipe(p); + break; + case '#': + r = new rod(); + break; + } + + arr[y * W + x] = std::unique_ptr(std::move(r)); + } + + return reactor(arr, W, H); +} + diff --git a/src/reactor/builder.hpp b/src/reactor/builder.hpp index 16f2c84..79ba7ba 100644 --- a/src/reactor/builder.hpp +++ b/src/reactor/builder.hpp @@ -12,43 +12,7 @@ namespace sim::reactor { -template -reactor builder(fuel::fuel_rod fr, control::control_rod cr, coolant::pipe p, std::array lines) -{ - std::array arr; - - for(int y = 0; y < H; y++) - for(int x = 0; x < W; x++) - { - char c = lines[y][x]; - rod* r; - - switch(c) - { - case 'F': - r = new fuel::fuel_rod(fr); - break; - case 'C': - r = new control::control_rod(cr); - break; - case 'G': - r = new control::graphite_rod(); - break; - case 'H': - r = new coolant::heater(); - break; - case ' ': - r = new coolant::pipe(p); - break; - case '#': - r = new rod(); - } - - arr[y * W + x] = r; - } - - return reactor(arr); -} +reactor builder(const int W, const int H, fuel::fuel_rod fr, control::control_rod cr, coolant::pipe p, const char** lines); }; diff --git a/src/reactor/reactor.cpp b/src/reactor/reactor.cpp new file mode 100644 index 0000000..be7991e --- /dev/null +++ b/src/reactor/reactor.cpp @@ -0,0 +1,123 @@ + +#include "reactor.hpp" + +#include + +using namespace sim::reactor; + +reactor::reactor(std::unique_ptr* rods, int width, int height) : width(width), height(height), size(width * height) +{ + this->rods = new std::unique_ptr[width * height]; + + for(int i = 0; i < size; i++) + { + this->rods[i] = std::move(rods[i]); + } +} + +reactor::reactor(reactor&& o) : width(o.width), height(o.height), size(o.size) +{ + rods = o.rods; + cursor = o.cursor; + o.rods = nullptr; +} + +reactor::~reactor() +{ + if(rods != nullptr) + { + delete[] rods; + } +} + +void reactor::update(std::mt19937& rand, double secs) +{ + int rods_lookup[size]; + + for(int i = 0; i < size; i++) + { + rods_lookup[i] = i; + } + + for(int i = 0; i < size; i++) + { + rods[i]->update(secs); + } + + update_interactions(rand, rods_lookup, secs / 2); +} + +void reactor::update_selected(int v) +{ + for(int i = 0; i < size; i++) + { + rod* r = rods[i].get(); + + if(r->is_selected()) + { + r->update_rod_selected(v); + } + } +} + +int reactor::move_cursor(int d) +{ + for(int i = 0; i < size; i++) + { + cursor = (cursor + d) % size; + + if(cursor < 0) + { + cursor += size; + } + + if(rods[cursor]->should_select()) + { + return cursor; + } + } + + return 0; +} + +void reactor::toggle_selected() +{ + if(rods[cursor]->should_select()) + { + rods[cursor]->toggle_selected(); + } +} + +void reactor::update_tile(std::mt19937& rand, double secs, int i, int x, int y) +{ + int nb_lookup[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; + std::shuffle(nb_lookup, &nb_lookup[3], rand); + + for(int j = 0; j < 4; j++) + { + int xp = x + nb_lookup[j][0]; + int yp = y + nb_lookup[j][1]; + + if(xp >= 0 && yp >= 0 && xp < width && yp < height) + { + rods[i]->interact(rods[yp * width + xp].get(), secs / 2); + } + } +} + +void reactor::update_interactions(std::mt19937& rand, int* rods_lookup, double secs) +{ + std::shuffle(rods_lookup, &rods_lookup[size - 1], rand); + + for(int id = 0; id < size; id++) + { + int i = rods_lookup[id]; + int x = i % width; + int y = i / width; + + for(int j = 0; j < 4; j++) + { + update_tile(rand, secs, i, x, y); + } + } +} diff --git a/src/reactor/reactor.hpp b/src/reactor/reactor.hpp index 856fba4..aecd1ab 100644 --- a/src/reactor/reactor.hpp +++ b/src/reactor/reactor.hpp @@ -3,126 +3,36 @@ #include "rod.hpp" -#include #include -#include #include +#include +#include namespace sim::reactor { -template struct reactor { - constexpr const static int width = W; - constexpr const static int height = H; - constexpr const static int size = W*H; - - rod* rods[size]; + const int width; + const int height; + const int size; + std::unique_ptr* rods; int cursor = 0; - reactor(std::array rods) - { - for(int i = 0; i < size; i++) - { - this->rods[i] = rods[i]; - } - } - - void update(std::mt19937& rand, double secs) - { - int rods_lookup[size]; - - for(int i = 0; i < size; i++) - { - rods_lookup[i] = i; - } - - for(int i = 0; i < size; i++) - { - rods[i]->update(secs); - } - - update_interactions(rand, rods_lookup, secs / 2); - } - - void update_selected(int v) - { - for(int i = 0; i < size; i++) - { - rod* r = rods[i]; - - if(r->is_selected()) - { - r->update_rod_selected(v); - } - } - } - - int move_cursor(int d) - { - for(int i = 0; i < size; i++) - { - cursor = (cursor + d) % size; - - if(cursor < 0) - { - cursor += size; - } - - if(rods[cursor]->should_select()) - { - return cursor; - } - } - - return 0; - } - - void toggle_selected() - { - if(rods[cursor]->should_select()) - { - rods[cursor]->toggle_selected(); - } - } + reactor(std::unique_ptr* rods, int width, int height); + reactor(reactor&& r); + ~reactor(); + + void update(std::mt19937& rand, double secs); + void update_selected(int v); + int move_cursor(int d); + void toggle_selected(); private: - void update_tile(std::mt19937& rand, double secs, int i, int x, int y) - { - std::array nb_lookup[4] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; - std::shuffle(nb_lookup, &nb_lookup[3], rand); - - for(int j = 0; j < 4; j++) - { - int xp = x + nb_lookup[j][0]; - int yp = y + nb_lookup[j][1]; - - if(xp >= 0 && yp >= 0 && xp < width && yp < height) - { - rods[i]->interact(rods[yp * width + xp], secs / 2); - } - } - } - - void update_interactions(std::mt19937& rand, int* rods_lookup, double secs) - { - std::shuffle(rods_lookup, &rods_lookup[size - 1], rand); - - for(int id = 0; id < size; id++) - { - int i = rods_lookup[id]; - int x = i % width; - int y = i / width; - - for(int j = 0; j < 4; j++) - { - update_tile(rand, secs, i, x, y); - } - } - } + void update_tile(std::mt19937& rand, double secs, int i, int x, int y); + void update_interactions(std::mt19937& rand, int* rods_lookup, double secs); }; }