#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include 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) ) psemek_declare_enum(card_type, std::uint32_t, (crossing) (furnace) (factory) (zoomer) ) 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 inputs; resource_type output; }; static util::hash_map> 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 const neighbours[4] { {1, 0}, {0, 1}, {-1, 0}, {0, -1}, }; struct location { int level; geom::point coords; geom::point center() const { float s = std::pow(3.f, -level); return {(coords[0] + 0.5f) * s, (coords[1] + 0.5f) * s}; } geom::box 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 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 belts_to = {}; boost::container::flat_set belts_from = {}; }; void add_belt(ecs::container & world, ecs::handle from, ecs::handle to) { world.get(from).get().belts_to.insert(to); world.get(to).get().belts_from.insert(from); } void remove_belt(ecs::container & world, ecs::handle from, ecs::handle to) { world.get(from).get().belts_to.erase(to); world.get(to).get().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 position(ecs::container & world, item const & i) { if (i.target) { auto end = world.get(i.target).get().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().location; return 3.f * std::pow(3.f, -geom::lerp(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; } bool within_tile(location const & tile, location const & l) { if (l.level < tile.level) return false; int s = std::pow(3, l.level - tile.level); geom::interval xrange{tile.coords[0] * s, (tile.coords[0] + 1) * s - 1}; geom::interval yrange{tile.coords[1] * s, (tile.coords[1] + 1) * s - 1}; return geom::contains(xrange, l.coords[0]) && geom::contains(yrange, l.coords[1]); } struct lab { psemek_ecs_declare_uuid("lab") }; struct map { ecs::container world; util::hash_map cards = {}; int stage = 0; int resource_count = 0; float spawn_timer = 0.f; bool take_card(card_type type) { if (!cards.contains(type)) return false; if (cards.at(type) == 0) return false; cards.at(type) -= 1; return true; } void put_card(card_type type) { cards[type] += 1; } }; struct stage_info { resource_type type; int count; std::vector cards; std::vector sources; }; static stage_info stages[] { {resource_type::stone, 0, {}, {resource_type::stone}}, {resource_type::stone, 10, {card_type::furnace}, {resource_type::coal}}, {resource_type::stone_brick, 30, {card_type::furnace}, {resource_type::iron_ore}}, {resource_type::iron_plate, 30, {card_type::factory, card_type::crossing, card_type::crossing}, {resource_type::copper_ore}}, {resource_type::red_science_pack, 30, {card_type::furnace, card_type::factory, card_type::crossing, card_type::crossing, card_type::zoomer, card_type::zoomer, card_type::zoomer}, {}}, {resource_type::inserter, 60, {card_type::factory, card_type::factory, card_type::factory, card_type::factory, card_type::crossing, card_type::crossing, card_type::zoomer, card_type::zoomer, card_type::zoomer}, {resource_type::coal}}, {resource_type::green_science_pack, 60, {}, {}}, }; template struct index_base { static constexpr util::uuid uuid() { return UUID; } index_base(ecs::container & world) : world_(world) { world.apply([this](ecs::handle entity, Component const & v){ index_[v.location] = entity; }); world.constructor([this](ecs::handle entity, Component const & v){ index_[v.location] = entity; }); world.destructor([this](Component const & v){ index_.erase(v.location); }); } std::optional 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 index_; }; using index = index_base; using path_index = index_base; void sink_belt(ecs::container & world, location m) { if (auto entity = world.index().find(m.up()); !entity || !world.get(*entity).contains()) return; auto & index = world.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(); if (cv.belts_to.contains(me)) { auto & mv = world.get(me).get(); 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(); 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().find(m.up()); !entity || !world.get(*entity).contains()) return; auto d = m.coords - c.coords; c = m.down(); m = c.moved(d); sink(world, c, m); } void clear_tile(map & map, location const & l) { auto entity = map.world.index().find(l); if (!entity) return; auto acc = map.world.get(*entity); if (acc.contains() || acc.contains()) return; if (acc.contains()) map.put_card(card_type::crossing); else if (acc.contains()) { map.put_card(card_type::zoomer); // TODO: CLEAR ZOOMER for (int y = 0; y < 3; ++y) { for (int x = 0; x < 3; ++x) { location p{l.level + 1, {3 * l.coords[0] + x, 3 * l.coords[1] + y}}; clear_tile(map, p); } } auto & index = map.world.index(); for (int y = 0; y < 9; ++y) { for (int x = 0; x < 9; ++x) { location p{l.level + 2, {9 * l.coords[0] + x, 9 * l.coords[1] + y}}; if (auto e = index.find(p)) { auto & v = map.world.get(*e).get(); for (auto to : v.belts_to) { auto & tv = map.world.get(to).get(); if (!within_tile(l, tv.location)) remove_belt(map.world, to, *tv.belts_to.begin()); remove_belt(map.world, *e, to); } for (auto from : v.belts_from) { auto & fv = map.world.get(from).get(); if (!within_tile(l, fv.location)) remove_belt(map.world, *fv.belts_from.begin(), from); remove_belt(map.world, from, *e); } } } } } else if (auto t = acc.get_if()) { if (t->type == transformer_type::furnace) map.put_card(card_type::furnace); else if (t->type == transformer_type::factory) map.put_card(card_type::factory); } map.world.destroy(*entity); } map starting_map() { map result; result.world.index(); result.world.index(); result.world.create( vertex{{0, {1, -1}}}, lab{} ); return result; } void draw_grid(geom::box const & box, float view_level, gfx::painter & painter, bool solid = false) { float const grid_width = 0.05f * std::min(box[0].length() / 3.f, std::pow(3.f, -1.f - view_level)); gfx::color_rgba color = gfx::black; if (!solid) color = gfx::light(color, 0.75f); for (int x = 0; x <= 3; ++x) { if (solid) { painter.line(box.corner(x / 3.f, 0.f), box.corner(x / 3.f, 1.f), grid_width, color, true); painter.line(box.corner(0.f, x / 3.f), box.corner(1.f, x / 3.f), grid_width, color, true); } else { for (int i = 0; i < 7; ++i) { float s = (i - 1.f / 3.f) / 6.f; float t = (i + 1.f / 3.f) / 6.f; s = std::max(s, 0.f); t = std::min(t, 1.f); painter.line(box.corner(x / 3.f, s), box.corner(x / 3.f, t), grid_width, color, true); painter.line(box.corner(s, x / 3.f), box.corner(t, x / 3.f), grid_width, color, true); } } } } void draw_item(resource_type type, geom::point 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::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::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_structure(geom::box const & bbox, transformer const & t, gfx::painter & painter) { auto box = geom::shrink(bbox, bbox[0].length() * 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 (t.type) { case transformer_type::furnace: color = color_of(resource_type::stone); break; case transformer_type::factory: color = color_of(resource_type::iron_ore); break; } painter.triangle(p0, p1, p2, color); painter.triangle(p0, p2, p3, color); } void draw_structure(geom::box const & bbox, crossing const &, gfx::painter & painter) { auto wbox = geom::shrink(bbox, bbox[0].length() * 0.2f); auto sbox = geom::shrink(bbox, bbox[0].length() * 0.3f); gfx::color_rgba color{64, 64, 64, 255}; painter.rect({wbox[0], sbox[1]}, color); painter.rect({sbox[0], wbox[1]}, color); } void draw_card(geom::box const & bbox, card_type type, gfx::painter & painter) { switch (type) { case card_type::crossing: draw_structure(bbox, crossing{}, painter); break; case card_type::furnace: draw_structure(bbox, transformer{transformer_type::furnace}, painter); break; case card_type::factory: draw_structure(bbox, transformer{transformer_type::factory}, painter); break; case card_type::zoomer: draw_grid(bbox, -1.f, painter, true); break; } } void draw(map & map, float view_level, gfx::painter & painter) { draw_grid({{{0.f, 3.f}, {0.f, 3.f}}}, view_level, painter); map.world.apply([&](vertex const & v, zoomer const &) { draw_grid(v.location.bbox(), view_level, painter); }); map.world.apply([&](path_vertex const & vertex) { for (auto b : vertex.belts_to) { auto q = map.world.get(b).get().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 & vertex) { for (auto b : vertex.belts_to) { auto q = map.world.get(b).get().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 & 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}}); }); map.world.apply([&](vertex const & v, transformer const & t) { draw_structure(v.location.bbox(), t, painter); }); map.world.apply([&](vertex const & v, crossing const & c) { draw_structure(v.location.bbox(), c, painter); }); map.world.apply([&](vertex const & v, lab const &) { 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; draw_item(stages[map.stage].type, pen, 1.f, painter); pen[1] -= 24.f * vs; painter.text(pen, std::format("{}/{}", map.resource_count, stages[map.stage].count), {.scale = {vs, -vs}, .c = {0, 0, 0, 255}}); }); map.world.apply([&](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, bool solid = false) { auto b = l.bbox(); float w = std::pow(3.f, -l.level) * 0.05f; if (solid) { painter.line(b.corner(0, 0), b.corner(1, 0), w, color, true); painter.line(b.corner(1, 0), b.corner(1, 1), w, color, true); painter.line(b.corner(1, 1), b.corner(0, 1), w, color, true); painter.line(b.corner(0, 1), b.corner(0, 0), w, color, true); } else { for (int i = 0; i <= 2; ++i) { float s = (i - 1.f / 3.f) / 2.f; float t = (i + 1.f / 3.f) / 2.f; s = std::max(s, 0.f); t = std::min(t, 1.f); painter.line(b.corner(s, 0), b.corner(t, 0), w, color, true); painter.line(b.corner(s, 1), b.corner(t, 1), w, color, true); painter.line(b.corner(0, s), b.corner(0, t), w, color, true); painter.line(b.corner(1, s), b.corner(1, t), w, color, true); } } } std::uint64_t make_seed() { random::device d; return (std::uint64_t(d()) << 32) | d(); } struct application : app::application { application(options const &, context const &) : seed_(make_seed()) , map_rng_{seed_, 0xf24130ddef6fb31full} , item_rng_{seed_, 0xb9fc3979f9860bbdull} , map_(starting_map()) { log::info() << "Starting seed: " << seed_; 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_wheel_event const & event) override { if (event.delta > 0) { if (selected_ && !view_transition_) { bool transitioned = false; if (auto entity = map_.world.index().find(*selected_)) if (map_.world.get(*entity).contains()) { 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 (event.delta < 0) { if (!view_transition_ && view_stack_.size() > 1) { view_transition_ = {view_stack_.back()}; view_stack_.pop_back(); selected_ = std::nullopt; } } } 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) { lmb_down_ = true; if (!selected_item_ && selected_ && !belt_start_) { belt_start_ = *selected_; } } if (!event.down && event.button == app::mouse_button::left) { lmb_down_ = false; 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(); 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(); 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; } } void on_event(app::key_event const & event) override { if (event.down && event.key == app::keycode::F1) { for (auto type : card_type_values()) map_.cards[type] += 10; } if (event.down && event.key == app::keycode::F) { if (selected_ && !map_.world.index().find(*selected_)) { if (map_.take_card(card_type::furnace)) map_.world.create( vertex{*selected_}, transformer{transformer_type::furnace} ); } } if (event.down && event.key == app::keycode::G) { if (selected_ && !map_.world.index().find(*selected_)) { if (map_.take_card(card_type::factory)) map_.world.create( vertex{*selected_}, transformer{transformer_type::factory} ); } } if (event.down && event.key == app::keycode::T) { if (selected_ && !map_.world.index().find(*selected_)) { if (map_.take_card(card_type::crossing)) map_.world.create( vertex{*selected_}, crossing{} ); } } if (event.down && event.key == app::keycode::Z) { if (selected_ && !map_.world.index().find(*selected_)) { if (map_.take_card(card_type::zoomer)) { 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_) { clear_tile(map_, *selected_); } } } bool running() const override { return running_; } void stop() override { running_ = false; } void update() override { float const dt = clock_.restart().count(); map_.spawn_timer += 3.f * dt; if (map_.spawn_timer >= 1.f) { map_.spawn_timer -= 1.f; map_.world.apply( [&](vertex const & v, source const & s) { auto p = v.location.down(); auto t = map_.world.index().get(p); if (!map_.world.get(t).contains()) { auto i = map_.world.create( item{s.type, p} ); map_.world.attach(t, occupied{i}); } } ); } if (map_.stage + 1 < std::size(stages) && map_.resource_count >= stages[map_.stage].count) { for (auto card : stages[map_.stage].cards) map_.cards[card] += 1; for (auto type : stages[map_.stage].sources) { util::hash_set spots; auto add = [&](location l) { if (!map_.world.index().find(l)) spots.insert(l); }; for (int i = 0; i < 3; ++i) { add({0, {-1, i}}); add({0, {3, i}}); add({0, {i, 3}}); } if (!spots.empty()) { map_.world.create( vertex{random::uniform_from(map_rng_, spots)}, source{type} ); } } map_.stage += 1; map_.resource_count = 0; } map_.world.apply([&](vertex const & v, transformer const & t) { boost::container::flat_map has_inputs; auto c = v.location.down(); auto ce = map_.world.index().get(c); if (map_.world.get(ce).contains()) return; for (auto n : neighbours) { auto p = c.moved(n); if (auto ne = map_.world.index().find(p)) if (map_.world.get(*ne).get().belts_to.contains(ce)) if (auto occ = map_.world.get(*ne).get_if()) if (auto i = map_.world.get(occ->entity).get_if()) 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().get(l); auto re = map_.world.get(e).get().entity; map_.world.detach(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; } }); map_.world.apply([&](ecs::handle entity, item & i) { if (i.target) { { auto & v = map_.world.get(i.target).get(); if (v.belts_from.empty() && v.belts_to.empty()) { map_.world.detach(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().location; map_.world.detach(i.target); i.target = ecs::handle::null(); } else { auto s = map_.world.index().get(i.start); map_.world.detach(s); auto & v = map_.world.get(s).get(); if (v.belts_to.empty() && v.belts_from.empty()) { map_.world.destroy(entity); return; } } if (auto cell = map_.world.index().find(i.start.up())) { if (map_.world.get(*cell).get_if()) { if (i.type == stages[map_.stage].type) { map_.resource_count += 1; map_.world.destroy(entity); } else { map_.world.attach(map_.world.index().get(i.start), occupied{entity}); } return; } if (map_.world.get(*cell).contains()) { auto c = i.start.up().down(); auto d = c.coords - i.start.coords; auto n = c.moved(d); auto & index = map_.world.index(); auto se = index.get(i.start); auto ce = index.get(c); auto ne = index.get(n); if (map_.world.get(se).get().belts_to.contains(ce)) { if (map_.world.get(ce).get().belts_to.contains(ne) && !map_.world.get(ne).contains()) { i.target = ne; map_.world.attach(ne, occupied{entity}); } else { map_.world.attach(se, occupied{entity}); } return; } } if (map_.world.get(*cell).get_if()) { auto se = map_.world.index().get(i.start); auto ce = map_.world.index().get(i.start.up().down()); if (map_.world.get(se).get().belts_to.contains(ce)) { map_.world.attach(map_.world.index().get(i.start), occupied{entity}); return; } } } std::vector targets; for (auto b : map_.world.get(map_.world.index().get(i.start)).get().belts_to) if (!map_.world.get(b).contains()) targets.push_back(b); if (!targets.empty()) i.target = random::uniform_from(item_rng_, targets); if (i.target) map_.world.attach(i.target, occupied{entity}); else map_.world.attach(map_.world.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([&](ecs::handle entity, item const & i) { auto box = geom::expand(geom::box::singleton(position(map_.world, i)), scale(map_.world, i) / 9.f); if (geom::contains(box, m)) selected_item_ = entity; }); if (selected_item_ && lmb_down_) { auto const & i = map_.world.get(*selected_item_).get(); if (i.target) map_.world.detach(i.target); else map_.world.detach(map_.world.index().get(i.start)); map_.world.destroy(*selected_item_); selected_item_ = std::nullopt; } { 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().find(p.up())) if (map_.world.get(*entity).contains()) break; selected_ = std::nullopt; break; // TODO: selected_ = selected_->up(); } } else if (!within_grid(p)) if (auto entity = map_.world.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(view_transition_->old.level, view_stack_.back().level, t); } draw(map_, view_level, painter_); if (selected_) draw_selection(*selected_, painter_, {255, 128, 128, 255}); if (belt_start_) draw_selection(*belt_start_, painter_, {255, 191, 128, 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(); draw_item(i.type, position(map_.world, i), scale(map_.world, i), painter_, true); } if (selected_) if (auto entity = map_.world.index().find(*selected_)) if (auto t = map_.world.get(*entity).get_if()) { float const scale = std::pow(3.f, -1.f - view_level); float const step = 1.5f * scale / 9.f; geom::point 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; } } { float const step = std::pow(3.f, - 2.f - view_level); float vs = std::pow(3.f, - 1.f - view_level) * 0.01f; geom::point pen = view_box_.corner(1, 1) + geom::vector{-1.f, -1.f} * view_box_[1].length() / 10.f; for (auto type : card_type_values()) { if (!map_.cards.contains(type)) continue; draw_card(geom::expand(geom::box::singleton(pen), step), type, painter_); painter_.text(pen, std::to_string(map_.cards.at(type)), {.scale = {vs, -vs}, .c = {0, 0, 0, 255}}); pen[1] -= step * 2.f; } } painter_.render(geom::orthographic_camera{view_box_}.transform()); } private: bool running_ = true; std::uint64_t seed_; random::generator map_rng_; random::generator item_rng_; map map_; util::clock<> clock_; geom::vector screen_size_{1, 1}; geom::point mouse_{0, 0}; bool lmb_down_ = false; gfx::painter painter_; struct view_transition { location old; float timer = 0.f; }; std::optional view_transition_; std::vector view_stack_; geom::box view_box_; std::optional selected_; std::optional belt_start_; std::optional selected_item_; }; } namespace psemek::app { std::unique_ptr make_application_factory() { application::options options { .name = "GMTK 2024", }; return default_application_factory(options); } }