moved reactor and builder to their own files

This commit is contained in:
Jay Robson 2024-01-26 13:27:29 +11:00
parent 6f4dba097e
commit ea4b41f564
7 changed files with 198 additions and 155 deletions

View File

@ -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) static void proc_mesh(proc_state& state, glm::mat4 mat, aiMesh* mesh, const aiScene* scene)
{ {
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex]; 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; unsigned int offset = state.offset;
for(unsigned int i = 0; i < mesh->mNumVertices; i++) 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]; auto [x, y, z] = mesh->mVertices[i];
vertex.pos = glm::vec4(x, y, z, 1) * mat; vertex.pos = glm::vec4(x, y, z, 1) * mat;
vertex.texid = texid; vertex.texid = handle;
if(mesh->HasNormals()) if(mesh->HasNormals())
{ {

View File

@ -10,23 +10,26 @@
using namespace sim; using namespace sim;
reactor::coolant::vessel* parts::vessel; reactor::coolant::vessel* parts::vessel;
reactor::reactor<5, 5>* parts::reactor; reactor::reactor* parts::reactor;
coolant::valve<sim::reactor::coolant::vessel>* parts::valve; coolant::valve<sim::reactor::coolant::vessel>* parts::valve;
coolant::pump<sim::reactor::coolant::vessel>* parts::pump; coolant::pump<sim::reactor::coolant::vessel>* parts::pump;
void parts::init() void parts::init()
{ {
vessel = new reactor::coolant::vessel(8, 10, 300, sim::coolant::WATER); const char* layout[] = {
reactor = new reactor::reactor<5, 5>(sim::reactor::builder<5, 5>(
reactor::fuel::fuel_rod(2000, 4000),
reactor::control::control_rod(*vessel, 10000, 1),
reactor::coolant::pipe(*vessel), {
"#C#C#", "#C#C#",
"CFCFC", "CFCFC",
"#C#C#", "#C#C#",
"CFCFC", "CFCFC",
"#C#C#" "#C#C#"
})); };
vessel = new reactor::coolant::vessel(8, 10, 300, sim::coolant::WATER);
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),
layout));
valve = new coolant::valve<reactor::coolant::vessel>(*vessel, 1, 500); valve = new coolant::valve<reactor::coolant::vessel>(*vessel, 1, 500);
pump = new coolant::pump<reactor::coolant::vessel>(*vessel, 1e4, 15); pump = new coolant::pump<reactor::coolant::vessel>(*vessel, 1e4, 15);

View File

@ -10,7 +10,7 @@ namespace sim::parts
{ {
extern sim::reactor::coolant::vessel* vessel; extern sim::reactor::coolant::vessel* vessel;
extern sim::reactor::reactor<5, 5>* reactor; extern sim::reactor::reactor* reactor;
extern sim::coolant::valve<sim::reactor::coolant::vessel>* valve; extern sim::coolant::valve<sim::reactor::coolant::vessel>* valve;
extern sim::coolant::pump<sim::reactor::coolant::vessel>* pump; extern sim::coolant::pump<sim::reactor::coolant::vessel>* pump;

43
src/reactor/builder.cpp Normal file
View File

@ -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<rod> 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<rod>(std::move(r));
}
return reactor(arr, W, H);
}

View File

@ -12,43 +12,7 @@
namespace sim::reactor namespace sim::reactor
{ {
template <int W, int H> reactor builder(const int W, const int H, fuel::fuel_rod fr, control::control_rod cr, coolant::pipe p, const char** lines);
reactor<W, H> builder(fuel::fuel_rod fr, control::control_rod cr, coolant::pipe p, std::array<const char*, H> lines)
{
std::array<rod*, W * H> 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<W, H>(arr);
}
}; };

123
src/reactor/reactor.cpp Normal file
View File

@ -0,0 +1,123 @@
#include "reactor.hpp"
#include <algorithm>
using namespace sim::reactor;
reactor::reactor(std::unique_ptr<rod>* rods, int width, int height) : width(width), height(height), size(width * height)
{
this->rods = new std::unique_ptr<rod>[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);
}
}
}

View File

@ -3,126 +3,36 @@
#include "rod.hpp" #include "rod.hpp"
#include <array>
#include <random> #include <random>
#include <algorithm>
#include <iostream> #include <iostream>
#include <memory>
#include <vector>
namespace sim::reactor namespace sim::reactor
{ {
template <int W, int H>
struct reactor struct reactor
{ {
constexpr const static int width = W; const int width;
constexpr const static int height = H; const int height;
constexpr const static int size = W*H; const int size;
rod* rods[size];
std::unique_ptr<rod>* rods;
int cursor = 0; int cursor = 0;
reactor(std::array<rod*, size> rods) reactor(std::unique_ptr<rod>* rods, int width, int height);
{ reactor(reactor&& r);
for(int i = 0; i < size; i++) ~reactor();
{
this->rods[i] = rods[i];
}
}
void update(std::mt19937& rand, double secs) void update(std::mt19937& rand, double secs);
{ void update_selected(int v);
int rods_lookup[size]; int move_cursor(int d);
void toggle_selected();
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();
}
}
private: private:
void update_tile(std::mt19937& rand, double secs, int i, int x, int 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);
std::array<int, 2> 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);
}
}
}
}; };
} }