color-fractory/source/application.cpp
2024-08-19 01:09:36 +03:00

1259 lines
31 KiB
C++

#include <psemek/app/default_application_factory.hpp>
#include <psemek/gfx/gl.hpp>
#include <psemek/gfx/painter.hpp>
#include <psemek/util/array.hpp>
#include <psemek/random/generator.hpp>
#include <psemek/random/device.hpp>
#include <psemek/random/uniform.hpp>
#include <psemek/util/hash_table.hpp>
#include <psemek/util/enum.hpp>
#include <psemek/util/clock.hpp>
#include <psemek/geom/box.hpp>
#include <psemek/geom/camera.hpp>
#include <psemek/geom/contains.hpp>
#include <psemek/ecs/container.hpp>
#include <psemek/ecs/declare_uuid.hpp>
#include <psemek/log/log.hpp>
#include <boost/container/flat_set.hpp>
#include <boost/container/flat_map.hpp>
#include <format>
namespace gmtk
{
using namespace psemek;
psemek_declare_enum(resource_type, std::uint32_t,
(stone)
(coal)
(iron_ore)
(copper_ore)
(stone_brick)
(iron_plate)
(copper_plate)
(iron_gear)
(copper_wire)
(inserter)
(electric_circuit)
(red_science_pack)
(green_science_pack)
)
psemek_declare_enum(transformer_type, std::uint32_t,
(furnace)
(factory)
)
gfx::color_rgba color_of(resource_type c)
{
switch (c)
{
case resource_type::stone: return {144, 128, 96, 255};
case resource_type::coal: return {32, 32, 32, 255};
case resource_type::iron_ore: return {96, 128, 144, 255};
case resource_type::copper_ore: return {224, 128, 64, 255};
case resource_type::stone_brick: return color_of(resource_type::stone);
case resource_type::iron_plate: return color_of(resource_type::iron_ore);
case resource_type::copper_plate: return color_of(resource_type::copper_ore);
case resource_type::iron_gear: return color_of(resource_type::iron_ore);
case resource_type::copper_wire: return color_of(resource_type::copper_ore);
case resource_type::inserter: return {224, 192, 64, 255};
case resource_type::electric_circuit: return {64, 192, 64, 255};
case resource_type::red_science_pack: return {192, 64, 64, 255};
case resource_type::green_science_pack: return {64, 192, 64, 255};
}
throw util::unknown_enum_value_exception{c};
}
struct recipe
{
util::hash_set<resource_type> inputs;
resource_type output;
};
static util::hash_map<transformer_type, std::vector<recipe>> const recipies
{
{
transformer_type::furnace,
{
{{resource_type::coal, resource_type::stone}, resource_type::stone_brick},
{{resource_type::coal, resource_type::iron_ore}, resource_type::iron_plate},
{{resource_type::coal, resource_type::copper_ore}, resource_type::copper_plate},
},
},
{
transformer_type::factory,
{
{{resource_type::iron_plate, resource_type::copper_plate}, resource_type::red_science_pack},
{{resource_type::iron_plate, resource_type::stone_brick}, resource_type::iron_gear},
{{resource_type::copper_plate, resource_type::stone_brick}, resource_type::copper_wire},
{{resource_type::iron_plate, resource_type::copper_wire}, resource_type::electric_circuit},
{{resource_type::iron_gear, resource_type::copper_plate}, resource_type::inserter},
{{resource_type::electric_circuit, resource_type::inserter, resource_type::red_science_pack}, resource_type::green_science_pack},
},
},
};
geom::vector<int, 2> const neighbours[4]
{
{1, 0},
{0, 1},
{-1, 0},
{0, -1},
};
struct location
{
int level;
geom::point<int, 2> coords;
geom::point<float, 2> center() const
{
float s = std::pow(3.f, -level);
return {(coords[0] + 0.5f) * s, (coords[1] + 0.5f) * s};
}
geom::box<float, 2> bbox(float extra = 0.f) const
{
float s = std::pow(3.f, -level);
return {{{(coords[0] - extra) * s, (coords[0] + 1.f + extra) * s}, {(coords[1] - extra) * s, (coords[1] + 1.f + extra) * s}}};
}
location left() const
{
return {level, {coords[0] - 1, coords[1]}};
}
location right() const
{
return {level, {coords[0] + 1, coords[1]}};
}
location bottom() const
{
return {level, {coords[0], coords[1] - 1}};
}
location top() const
{
return {level, {coords[0], coords[1] + 1}};
}
location moved(geom::vector<int, 2> const & delta) const
{
return {level, coords + delta};
}
location down() const
{
return {level + 1, {coords[0] * 3 + 1, coords[1] * 3 + 1}};
}
location up() const
{
return {level - 1, {geom::idiv(coords[0], 3), geom::idiv(coords[1], 3)}};
}
friend bool operator == (location const & x, location const & y) = default;
friend auto operator <=> (location const & x, location const & y) = default;
};
struct location_hash
{
std::size_t operator()(location const & x) const noexcept
{
return util::hash_all(x.level, x.coords[0], x.coords[1]);
}
};
struct vertex
{
psemek_ecs_declare_uuid("vertex")
struct location location;
};
struct source
{
psemek_ecs_declare_uuid("source")
resource_type type;
};
struct transformer
{
psemek_ecs_declare_uuid("transformer")
transformer_type type;
};
struct crossing
{
psemek_ecs_declare_uuid("crossing")
};
struct zoomer
{
psemek_ecs_declare_uuid("zoomer")
};
struct path_vertex
{
psemek_ecs_declare_uuid("path_vertex")
struct location location;
boost::container::flat_set<ecs::handle> belts_to = {};
boost::container::flat_set<ecs::handle> belts_from = {};
};
void add_belt(ecs::container & world, ecs::handle from, ecs::handle to)
{
world.get(from).get<path_vertex>().belts_to.insert(to);
world.get(to).get<path_vertex>().belts_from.insert(from);
}
void remove_belt(ecs::container & world, ecs::handle from, ecs::handle to)
{
world.get(from).get<path_vertex>().belts_to.erase(to);
world.get(to).get<path_vertex>().belts_from.erase(from);
}
struct occupied
{
psemek_ecs_declare_uuid("occupied")
ecs::handle entity;
};
struct item
{
psemek_ecs_declare_uuid("item")
resource_type type;
location start;
ecs::handle target = ecs::handle::null();
float state = 0.f;
};
geom::point<float, 2> position(ecs::container & world, item const & i)
{
if (i.target)
{
auto end = world.get(i.target).get<path_vertex const>().location;
return geom::lerp(i.start.center(), end.center(), i.state);
}
else
return i.start.center();
}
float scale(ecs::container & world, item const & i)
{
if (i.target)
{
auto end = world.get(i.target).get<path_vertex const>().location;
return 3.f * std::pow(3.f, -geom::lerp<float>(i.start.level, end.level, i.state));
}
else
return 3.f * std::pow(3.f, -i.start.level);
}
bool within_grid(location const & l)
{
int max = std::pow(3, l.level + 1);
return l.coords[0] >= 0 && l.coords[0] < max && l.coords[1] >= 0 && l.coords[1] < max;
}
struct timestamp
{
int trunc = 0;
float frac = 0.f;
timestamp & operator += (float dt)
{
frac += dt;
int t = std::floor(frac);
trunc += t;
frac -= t;
return *this;
}
friend auto operator <=> (timestamp const & x, timestamp const & y) = default;
};
struct lab
{
psemek_ecs_declare_uuid("lab")
util::hash_map<resource_type, int> count = {};
};
struct map
{
ecs::container world;
timestamp time = {};
float spawn_timer = 0.f;
};
template <typename Component, util::uuid UUID>
struct index_base
{
static constexpr util::uuid uuid()
{
return UUID;
}
index_base(ecs::container & world)
: world_(world)
{
world.apply<Component>([this](ecs::handle entity, Component const & v){
index_[v.location] = entity;
});
world.constructor<Component>([this](ecs::handle entity, Component const & v){
index_[v.location] = entity;
});
world.destructor<Component>([this](Component const & v){
index_.erase(v.location);
});
}
std::optional<ecs::handle> find(location const & l) const
{
if (auto it = index_.find(l); it != index_.end())
return it->second;
return std::nullopt;
}
ecs::handle get(location const & l) const
{
if (auto entity = find(l))
return *entity;
return world_.create(Component{l});
}
private:
ecs::container & world_;
util::hash_map<location, ecs::handle, location_hash> index_;
};
using index = index_base<vertex, util::make_uuid("vertex_index")>;
using path_index = index_base<path_vertex, util::make_uuid("path_vertex_index")>;
void sink_belt(ecs::container & world, location m)
{
if (auto entity = world.index<index>().find(m.up()); !entity || !world.get(*entity).contains<zoomer>())
return;
auto & index = world.index<path_index>();
auto c = m.up().down();
auto d = m.coords - c.coords;
auto ce = index.get(c);
auto me = index.get(m);
auto & cv = world.get(ce).get<path_vertex>();
if (cv.belts_to.contains(me))
{
auto & mv = world.get(me).get<path_vertex>();
auto ne = *mv.belts_to.begin();
remove_belt(world, ce, me);
remove_belt(world, me, ne);
auto cd = m.down();
auto md = cd.moved(d);
auto cde = index.get(cd);
auto mde = index.get(md);
add_belt(world, cde, mde);
add_belt(world, mde, ne);
sink_belt(world, md);
}
else if (cv.belts_from.contains(me))
{
auto & mv = world.get(me).get<path_vertex>();
auto ne = *mv.belts_from.begin();
remove_belt(world, ne, me);
remove_belt(world, me, ce);
auto cd = m.down();
auto md = cd.moved(d);
auto cde = index.get(cd);
auto mde = index.get(md);
add_belt(world, ne, mde);
add_belt(world, mde, cde);
sink_belt(world, md);
}
}
void sink(ecs::container & world, location & c, location & m)
{
if (auto entity = world.index<index>().find(m.up()); !entity || !world.get(*entity).contains<zoomer>())
return;
auto d = m.coords - c.coords;
c = m.down();
m = c.moved(d);
sink(world, c, m);
}
map starting_map(random::generator & rng)
{
map result;
result.world.index<index>();
result.world.index<path_index>();
result.world.create(
vertex{{0, {-1, 1}}},
source{resource_type::stone}
);
result.world.create(
vertex{{0, {-1, 2}}},
source{resource_type::iron_ore}
);
result.world.create(
vertex{{0, {1, 3}}},
source{resource_type::coal}
);
result.world.create(
vertex{{0, {3, 2}}},
source{resource_type::copper_ore}
);
result.world.create(
vertex{{0, {1, -1}}},
lab{}
);
(void)rng;
return result;
}
void draw_grid(location origin, float view_level, gfx::painter & painter, bool in_construction)
{
origin.level += 1;
origin.coords[0] *= 3;
origin.coords[1] *= 3;
float const grid_width = 0.1f * std::pow(3.f, -std::max<float>(view_level + 1.f, origin.level));
auto box = origin.bbox();
auto color = gfx::black.as_color_rgba();
if (in_construction)
color[3] = 127;
for (int x = 0; x <= 3; ++x)
{
painter.line(box.corner(x, 0), box.corner(x, 3), grid_width, color, true);
painter.line(box.corner(0, x), box.corner(3, x), grid_width, color, true);
}
}
void draw_item(resource_type type, geom::point<float, 2> const & pos, float scale, gfx::painter & painter, bool selected = false)
{
auto color = color_of(type);
gfx::color_rgba bcolor = selected ? gfx::red : gfx::black;
switch (type)
{
case resource_type::coal:
case resource_type::stone:
case resource_type::iron_ore:
case resource_type::copper_ore:
painter.circle(pos, 0.075f * scale, bcolor);
painter.circle(pos, 0.05f * scale, color);
break;
case resource_type::stone_brick:
case resource_type::iron_plate:
case resource_type::copper_plate:
{
auto box = geom::expand(geom::box<float, 2>::singleton(pos), 0.075f * scale);
painter.rect(box, bcolor);
box = geom::shrink(box, 0.025f * scale);
painter.rect(box, color);
}
break;
case resource_type::iron_gear:
case resource_type::copper_wire:
case resource_type::inserter:
case resource_type::electric_circuit:
{
bool first = true;
for (float radius : {0.075f * scale, 0.05f * scale})
{
auto c = first ? bcolor : color;
for (int i = 0; i < 6; ++i)
painter.triangle(pos, pos + geom::direction(geom::rad(i * 60.f)) * radius, pos + geom::direction(geom::rad((i + 1) * 60.f)) * radius, c);
first = false;
}
}
break;
case resource_type::red_science_pack:
case resource_type::green_science_pack:
{
auto box = geom::expand(geom::box<float, 2>::singleton(pos), 0.075f * scale);
painter.triangle(box.corner(0, 0), box.corner(1, 0), box.corner(0.5f, 1), bcolor);
box = geom::shrink(box, 0.025f * scale);
painter.triangle(box.corner(0, 0), box.corner(1, 0), box.corner(0.5f, 1), color);
}
break;
}
}
void draw(map & map, float view_level, gfx::painter & painter)
{
draw_grid({-1, {0, 0}}, view_level, painter, false);
map.world.apply<vertex const, zoomer const>([&](vertex const & v, zoomer const &)
{
draw_grid(v.location, view_level, painter, false);
});
map.world.apply<path_vertex const>([&](path_vertex const & vertex)
{
for (auto b : vertex.belts_to)
{
auto q = map.world.get(b).get<path_vertex const>().location;
float w0 = 0.3f * std::pow(3.f, 1.f - vertex.location.level);
float w1 = 0.3f * std::pow(3.f, 1.f - q.level);
gfx::color_rgba c{191, 191, 191, 255};
painter.line(vertex.location.center(), q.center(), w0, w1, c, c, true);
}
});
map.world.apply<path_vertex const>([&](path_vertex const & vertex)
{
for (auto b : vertex.belts_to)
{
auto q = map.world.get(b).get<path_vertex const>().location;
geom::vector d = q.center() - vertex.location.center();
auto c = vertex.location.center() + d / 2.f;
auto n = geom::ort(d);
float s = 1.f / 6.f;
d *= s * 0.5f;
n *= s;
painter.triangle(c - d + n, c - d - n, c + d, {255, 127, 0, 255});
}
});
map.world.apply<vertex const, source const>([&](vertex const & v, source const & s)
{
// float vs = std::pow(3.f, - v.location.level) * 0.01f;
painter.rect(v.location.bbox(-0.2f), color_of(s.type));
// painter.text(v.location.center(), "180/m", {.scale = {vs, -vs}, .c = {0, 0, 0, 255}});
});
auto draw_transformer = [&](location const & l, transformer_type type, bool in_construction)
{
auto box = l.bbox(-0.2f);
auto p0 = box.corner(0, 0);
auto p1 = box.corner(1, 0);
auto p2 = box.corner(0.8f, 1);
auto p3 = box.corner(0.2f, 1);
gfx::color_rgba color = {255, 0, 255, 255};
switch (type)
{
case transformer_type::furnace: color = color_of(resource_type::stone); break;
case transformer_type::factory: color = color_of(resource_type::iron_ore); break;
}
if (in_construction)
color[3] = 127;
painter.triangle(p0, p1, p2, color);
painter.triangle(p0, p2, p3, color);
};
map.world.apply<vertex const, transformer const>([&](vertex const & v, transformer const & t)
{
draw_transformer(v.location, t.type, false);
});
map.world.apply<vertex const, crossing const>([&](vertex const & v, crossing const &)
{
auto wbox = v.location.bbox(-0.2f);
auto sbox = v.location.bbox(-0.3f);
gfx::color_rgba color{64, 64, 64, 255};
painter.rect({wbox[0], sbox[1]}, color);
painter.rect({sbox[0], wbox[1]}, color);
});
map.world.apply<vertex const, lab const>([&](vertex const & v, lab const & l)
{
painter.rect(v.location.bbox(-0.2f), {128, 192, 255, 255});
auto pen = v.location.bbox(-0.4f).corner(0.5f, 1.f);
float vs = std::pow(3.f, - v.location.level) * 0.01f;
for (auto type : {resource_type::red_science_pack, resource_type::green_science_pack})
{
if (l.count.contains(type))
{
painter.text(pen, std::to_string(l.count.at(type)), {.scale = {vs, -vs}, .c = color_of(type)});
pen[1] -= 12.f * vs;
}
}
});
map.world.apply<item const>([&](item const & i)
{
draw_item(i.type, position(map.world, i), scale(map.world, i), painter);
});
}
void draw_selection(location const & l, gfx::painter & painter, gfx::color_rgba const & color)
{
auto b = l.bbox();
float s = std::pow(3.f, -l.level) * 0.1f;
painter.line(b.corner(0, 0), b.corner(1, 0), s, color, true);
painter.line(b.corner(1, 0), b.corner(1, 1), s, color, true);
painter.line(b.corner(1, 1), b.corner(0, 1), s, color, true);
painter.line(b.corner(0, 1), b.corner(0, 0), s, color, true);
}
struct application
: app::application
{
application(options const &, context const &)
: rng_{random::device{}}
, map_(starting_map(rng_))
{
view_stack_.push_back({-1, {0, 0}});
}
void on_event(app::resize_event const & event) override
{
screen_size_ = event.size;
}
void on_event(app::mouse_move_event const & event) override
{
mouse_ = event.position;
}
void on_event(app::mouse_button_event const & event) override
{
if (event.down && event.button == app::mouse_button::left)
{
bool destroyed_item = false;
bool transitioned = false;
if (selected_item_)
{
auto const & i = map_.world.get(*selected_item_).get<item const>();
if (i.target)
map_.world.detach<occupied>(i.target);
else
map_.world.detach<occupied>(map_.world.index<path_index>().get(i.start));
map_.world.destroy(*selected_item_);
selected_item_ = std::nullopt;
destroyed_item = true;
}
if (!destroyed_item && selected_ && !view_transition_)
{
if (auto entity = map_.world.index<index>().find(*selected_))
if (map_.world.get(*entity).contains<zoomer>())
{
view_transition_ = {view_stack_.back()};
view_stack_.push_back(*selected_);
selected_ = std::nullopt;
transitioned = true;
}
if (!transitioned && within_grid(*selected_) && selected_->level == view_stack_.back().level + 1 && selected_->up() != view_stack_.back())
{
view_transition_ = {view_stack_.back()};
view_stack_.back() = selected_->up();
selected_ = std::nullopt;
transitioned = true;
}
}
if (!destroyed_item && !transitioned && selected_ && !belt_start_)
{
belt_start_ = *selected_;
}
}
if (!event.down && event.button == app::mouse_button::left)
{
if (selected_ && belt_start_ && selected_->level == belt_start_->level)
{
auto d = selected_->coords - belt_start_->coords;
if (std::abs(d[0]) + std::abs(d[1]) == 1)
{
auto s = belt_start_->down();
location belt[4];
for (int i = 0; i <= 3; ++i)
belt[i] = s.moved(d * i);
sink(map_.world, belt[0], belt[1]);
sink(map_.world, belt[3], belt[2]);
auto & index = map_.world.index<path_index>();
for (int i = 0; i < 3; ++i)
{
auto p = belt[i];
auto q = belt[i + 1];
auto s = index.get(p);
auto t = index.get(q);
auto & sv = map_.world.get(s).get<path_vertex>();
if (sv.belts_to.contains(t))
remove_belt(map_.world, s, t);
else
{
remove_belt(map_.world, t, s);
add_belt(map_.world, s, t);
}
}
}
}
belt_start_ = std::nullopt;
}
if (event.down && event.button == app::mouse_button::right)
{
if (!view_transition_ && view_stack_.size() > 1)
{
view_transition_ = {view_stack_.back()};
view_stack_.pop_back();
selected_ = std::nullopt;
}
}
}
void on_event(app::key_event const & event) override
{
if (event.down && event.key == app::keycode::F)
{
if (selected_ && !map_.world.index<index>().find(*selected_))
{
map_.world.create(
vertex{*selected_},
transformer{transformer_type::furnace}
);
}
}
if (event.down && event.key == app::keycode::G)
{
if (selected_ && !map_.world.index<index>().find(*selected_))
{
map_.world.create(
vertex{*selected_},
transformer{transformer_type::factory}
);
}
}
if (event.down && event.key == app::keycode::T)
{
if (selected_ && !map_.world.index<index>().find(*selected_))
{
map_.world.create(
vertex{*selected_},
crossing{}
);
}
}
if (event.down && event.key == app::keycode::Z)
{
if (selected_ && !map_.world.index<index>().find(*selected_))
{
map_.world.create(
vertex{*selected_},
zoomer{}
);
auto c = selected_->down();
sink_belt(map_.world, c.left());
sink_belt(map_.world, c.right());
sink_belt(map_.world, c.bottom());
sink_belt(map_.world, c.top());
}
}
if (event.down && event.key == app::keycode::X)
{
if (selected_)
{
if (auto entity = map_.world.index<index>().find(*selected_))
{
auto acc = map_.world.get(*entity);
if (!acc.contains<source>() && !acc.contains<lab>() && !acc.contains<zoomer>())
map_.world.destroy(*entity);
}
}
}
}
bool running() const override
{
return running_;
}
void stop() override
{
running_ = false;
}
void update() override
{
float const dt = clock_.restart().count();
map_.time += dt;
map_.spawn_timer += 3.f * dt;
if (map_.spawn_timer >= 1.f)
{
map_.spawn_timer -= 1.f;
map_.world.apply<vertex const, source const>(
[&](vertex const & v, source const & s)
{
auto p = v.location.down();
auto t = map_.world.index<path_index>().get(p);
if (!map_.world.get(t).contains<occupied>())
{
auto i = map_.world.create(
item{s.type, p}
);
map_.world.attach(t, occupied{i});
}
}
);
}
map_.world.apply<vertex const, transformer const>([&](vertex const & v, transformer const & t)
{
boost::container::flat_map<resource_type, location> has_inputs;
auto c = v.location.down();
auto ce = map_.world.index<path_index>().get(c);
if (map_.world.get(ce).contains<occupied>())
return;
for (auto n : neighbours)
{
auto p = c.moved(n);
if (auto ne = map_.world.index<path_index>().find(p))
if (map_.world.get(*ne).get<path_vertex>().belts_to.contains(ce))
if (auto occ = map_.world.get(*ne).get_if<occupied const>())
if (auto i = map_.world.get(occ->entity).get_if<item const>())
if (i->start == p)
has_inputs[i->type] = p;
}
bool crafted = false;
for (auto const & recipe : recipies.at(t.type))
{
bool has_all = true;
for (auto type : recipe.inputs)
has_all &= has_inputs.contains(type);
if (!has_all)
continue;
for (auto type : recipe.inputs)
{
auto l = has_inputs.at(type);
auto e = map_.world.index<path_index>().get(l);
auto re = map_.world.get(e).get<occupied>().entity;
map_.world.detach<occupied>(e);
map_.world.destroy(re);
}
auto r = map_.world.create(
item{recipe.output, c}
);
map_.world.attach(ce, occupied{r});
crafted = true;
break;
}
if (!crafted && !has_inputs.empty())
{
return;
auto p = random::uniform_from(rng_, has_inputs);
auto e = map_.world.index<path_index>().get(p.second);
auto re = map_.world.get(e).get<occupied>().entity;
map_.world.detach<occupied>(e);
map_.world.destroy(re);
}
});
map_.world.apply<item>([&](ecs::handle entity, item & i)
{
if (i.target)
{
{
auto & v = map_.world.get(i.target).get<path_vertex const>();
if (v.belts_from.empty() && v.belts_to.empty())
{
map_.world.detach<occupied>(i.target);
map_.world.destroy(entity);
return;
}
}
i.state += 3.f * dt;
if (i.state < 1.f)
return;
i.state -= 1.f;
i.start = map_.world.get(i.target).get<path_vertex const>().location;
map_.world.detach<occupied>(i.target);
i.target = ecs::handle::null();
}
else
{
auto s = map_.world.index<path_index>().get(i.start);
map_.world.detach<occupied>(s);
auto & v = map_.world.get(s).get<path_vertex const>();
if (v.belts_to.empty() && v.belts_from.empty())
{
map_.world.destroy(entity);
return;
}
}
if (auto cell = map_.world.index<index>().find(i.start.up()))
{
if (auto l = map_.world.get(*cell).get_if<lab>())
{
l->count[i.type] += 1;
map_.world.destroy(entity);
return;
}
if (map_.world.get(*cell).contains<crossing>())
{
auto c = i.start.up().down();
auto d = c.coords - i.start.coords;
auto n = c.moved(d);
auto & index = map_.world.index<path_index>();
auto se = index.get(i.start);
auto ce = index.get(c);
auto ne = index.get(n);
if (map_.world.get(se).get<path_vertex>().belts_to.contains(ce))
{
if (map_.world.get(ce).get<path_vertex>().belts_to.contains(ne) && !map_.world.get(ne).contains<occupied>())
{
i.target = ne;
map_.world.attach(ne, occupied{entity});
}
else
{
map_.world.attach(se, occupied{entity});
}
return;
}
}
if (map_.world.get(*cell).get_if<transformer>())
{
auto se = map_.world.index<path_index>().get(i.start);
auto ce = map_.world.index<path_index>().get(i.start.up().down());
if (map_.world.get(se).get<path_vertex>().belts_to.contains(ce))
{
map_.world.attach(map_.world.index<path_index>().get(i.start), occupied{entity});
return;
}
}
}
std::vector<ecs::handle> targets;
for (auto b : map_.world.get(map_.world.index<path_index>().get(i.start)).get<path_vertex>().belts_to)
if (!map_.world.get(b).contains<occupied>())
targets.push_back(b);
if (!targets.empty())
i.target = random::uniform_from(rng_, targets);
if (i.target)
map_.world.attach(i.target, occupied{entity});
else
map_.world.attach(map_.world.index<path_index>().get(i.start), occupied{entity});
});
float aspect_ratio = (screen_size_[0] * 1.f) / screen_size_[1];
if (view_transition_)
{
auto from = view_transition_->old.bbox(1.f / 3.f);
auto to = view_stack_.back().bbox(1.f / 3.f);
float t = geom::smoothstep(view_transition_->timer * 2.f);
view_box_[0].min = geom::lerp(from[0].min, to[0].min, t);
view_box_[0].max = geom::lerp(from[0].max, to[0].max, t);
view_box_[1].min = geom::lerp(from[1].min, to[1].min, t);
view_box_[1].max = geom::lerp(from[1].max, to[1].max, t);
view_transition_->timer += dt;
if (view_transition_->timer >= 0.5f)
view_transition_ = std::nullopt;
}
else
{
view_box_ = view_stack_.back().bbox(1.f / 3.f);
}
view_box_[0] = geom::expand(view_box_[0], (view_box_[1].length() * aspect_ratio - view_box_[0].length()) / 2.f);
selected_ = std::nullopt;
selected_item_ = std::nullopt;
if (!view_transition_)
{
auto m = view_box_.corner(
mouse_[0] * 1.f / screen_size_[0],
1.f - mouse_[1] * 1.f / screen_size_[1]
);
map_.world.apply<item const>([&](ecs::handle entity, item const & i)
{
auto box = geom::expand(geom::box<float, 2>::singleton(position(map_.world, i)), scale(map_.world, i) / 9.f);
if (geom::contains(box, m))
selected_item_ = entity;
});
{
float s = std::pow(3.f, 1 + view_stack_.back().level);
m[0] *= s;
m[1] *= s;
auto l = view_stack_.back();
location p{1 + l.level, {std::floor(m[0]), std::floor(m[1])}};
geom::interval xrange{3 * l.coords[0], 3 * l.coords[0] + 2};
geom::interval yrange{3 * l.coords[1], 3 * l.coords[1] + 2};
auto xwrange = geom::expand(xrange, 1);
auto ywrange = geom::expand(yrange, 1);
if (geom::contains(xrange, p.coords[0]) && geom::contains(yrange, p.coords[1]))
selected_ = p;
else if (view_stack_.size() > 1 && within_grid(p) && ((geom::contains(xwrange, p.coords[0]) && geom::contains(yrange, p.coords[1])) || (geom::contains(xrange, p.coords[0]) && geom::contains(ywrange, p.coords[1]))))
{
selected_ = p;
while (selected_->level > 0)
{
if (auto entity = map_.world.index<index>().find(p.up()))
if (map_.world.get(*entity).contains<zoomer>())
break;
selected_ = std::nullopt;
break;
// TODO:
selected_ = selected_->up();
}
}
else if (!within_grid(p))
if (auto entity = map_.world.index<index>().find(p))
selected_ = p;
}
}
}
void present() override
{
gl::ClearColor(1.f, 1.f, 1.f, 1.f);
gl::Clear(gl::COLOR_BUFFER_BIT);
float view_level = view_stack_.back().level;
if (view_transition_)
{
float t = geom::smoothstep(view_transition_->timer * 2.f);
view_level = geom::lerp<float>(view_transition_->old.level, view_stack_.back().level, t);
}
draw(map_, view_level, painter_);
if (selected_)
draw_selection(*selected_, painter_, {255, 0, 0, 255});
if (belt_start_)
draw_selection(*belt_start_, painter_, {255, 255, 0, 255});
{
float w = (view_box_[0].length() - view_box_[1].length()) / 2.f;
geom::vector t{view_box_[1].length() / 5.f, 0.f};
geom::vector d{w - t[0], 0.f};
geom::vector n{w, 0.f};
auto p00 = view_box_.corner(0, 0);
auto p01 = view_box_.corner(0, 1);
auto p10 = view_box_.corner(1, 0);
auto p11 = view_box_.corner(1, 1);
gfx::color_rgba c1{255, 255, 255, 255};
gfx::color_rgba c0{255, 255, 255, 0};
painter_.triangle(p00, p00 + d, p01 + d, c1, c1, c1);
painter_.triangle(p00, p01 + d, p01, c1, c1, c1);
painter_.triangle(p00 + d, p00 + n, p01 + n, c1, c0, c0);
painter_.triangle(p00 + d, p01 + n, p01 + d, c1, c0, c1);
painter_.triangle(p10, p10 - d, p11 - d, c1, c1, c1);
painter_.triangle(p10, p11 - d, p11, c1, c1, c1);
painter_.triangle(p10 - d, p10 - n, p11 - n, c1, c0, c0);
painter_.triangle(p10 - d, p11 - n, p11 - d, c1, c0, c1);
}
if (selected_item_)
{
auto const & i = map_.world.get(*selected_item_).get<item const>();
draw_item(i.type, position(map_.world, i), scale(map_.world, i), painter_, true);
}
if (selected_)
if (auto entity = map_.world.index<index>().find(*selected_))
if (auto t = map_.world.get(*entity).get_if<transformer>())
{
float const scale = std::pow(3.f, -1.f - view_level);
float const step = 1.5f * scale / 9.f;
geom::point<float, 2> pen = view_box_.corner(0, 1) + geom::vector{1.f, -1.f} * view_box_[1].length() / 10.f;
for (auto const & recipe : recipies.at(t->type))
{
int i = 0;
for (auto type : recipe.inputs)
{
draw_item(type, pen + geom::vector{i * step, 0.f}, scale, painter_);
++i;
}
i = 3;
painter_.triangle(
pen + geom::vector{(i - 0.4f) * step, -0.4f * step},
pen + geom::vector{(i - 0.4f) * step, 0.4f * step},
pen + geom::vector{(i + 0.4f) * step, 0.f},
{127, 127, 127, 255}
);
i = 4;
draw_item(recipe.output, pen + geom::vector{i * step, 0.f}, scale, painter_);
pen[1] -= step;
}
}
painter_.render(geom::orthographic_camera{view_box_}.transform());
}
private:
bool running_ = true;
random::generator rng_;
map map_;
util::clock<> clock_;
geom::vector<int, 2> screen_size_{1, 1};
geom::point<int, 2> mouse_{0, 0};
gfx::painter painter_;
struct view_transition
{
location old;
float timer = 0.f;
};
std::optional<view_transition> view_transition_;
std::vector<location> view_stack_;
geom::box<float, 2> view_box_;
std::optional<location> selected_;
std::optional<location> belt_start_;
std::optional<ecs::handle> selected_item_;
};
}
namespace psemek::app
{
std::unique_ptr<application::factory> make_application_factory()
{
application::options options
{
.name = "GMTK 2024",
};
return default_application_factory<gmtk::application>(options);
}
}