Upgrade cellular client to v10 with readable play and assisted steering
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
// M9.8c toolkit-independent UI model: labels keep data states distinct, selection
|
||||
// follows presentations, the launcher bounds credentials and errors, and the outcome
|
||||
// log stays bounded.
|
||||
#include "cellular_controls.hpp"
|
||||
#include "cellular_play.hpp"
|
||||
#include "cellular_scene.hpp"
|
||||
#include "evo/client/labels.hpp"
|
||||
#include "evo/client/launcher.hpp"
|
||||
#include "evo/client/outcome_log.hpp"
|
||||
@@ -25,6 +28,11 @@ void check(bool ok, std::source_location at = std::source_location::current()) {
|
||||
throw std::runtime_error("client UI model line " + std::to_string(at.line()));
|
||||
}
|
||||
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
|
||||
template <class T> const T& required(const std::optional<T>& value) {
|
||||
if (!value)
|
||||
throw std::runtime_error("missing client model test value");
|
||||
return *value;
|
||||
}
|
||||
constexpr world::WorldIdentity identity{3, 9};
|
||||
|
||||
EnvironmentCell cell(std::int64_t x) {
|
||||
@@ -69,6 +77,56 @@ OrganismPresentation organisms(std::vector<OrganismCell> cells, std::uint64_t ti
|
||||
return std::move(*p);
|
||||
}
|
||||
|
||||
void cellular_assistance() {
|
||||
using namespace client_app;
|
||||
auto own = organism(42, 980);
|
||||
own.y = {2000};
|
||||
own.phenotype.movement_speed = 10000;
|
||||
auto population = organisms({own});
|
||||
network::CellularPerception view{2, 42, {{1000, 2000, 100, {}}}, {}};
|
||||
CellularControls controls;
|
||||
const Intention diagonal{1, 2, 707, -707};
|
||||
auto steering = steer_cellular(controls, diagonal, &view, &population, {});
|
||||
CHECK(steering.patch == std::pair<std::uint32_t, std::uint32_t>{1000, 2000});
|
||||
CHECK(steering.intention.move_x == 35 && steering.intention.move_y == -35);
|
||||
own.x = {979};
|
||||
population = organisms({own});
|
||||
CHECK(!steer_cellular(controls, diagonal, &view, &population, {}).patch);
|
||||
controls.slow_near_food = false;
|
||||
controls.precision = true;
|
||||
controls.speed_percent = 50;
|
||||
steering = steer_cellular(controls, diagonal, &view, &population, {});
|
||||
CHECK(steering.intention.move_x == 70 && steering.intention.move_y == -70);
|
||||
controls.auto_feed = false;
|
||||
CHECK(steer_cellular(controls, {}, &view, &population, {{9000, 9000}}).patch ==
|
||||
std::pair<std::uint32_t, std::uint32_t>{9000, 9000});
|
||||
controls.auto_feed = true;
|
||||
view.patches[0].energy_pj = 0;
|
||||
own.x = {1000};
|
||||
population = organisms({own});
|
||||
CHECK(!steer_cellular(controls, {}, &view, &population, {}).patch);
|
||||
view.patches[0].matter_fg[2] = 1;
|
||||
CHECK(steer_cellular(controls, {}, &view, &population, {}).patch);
|
||||
view.life_identity = 0;
|
||||
CHECK(!steer_cellular(controls, {}, &view, &population, {}).patch);
|
||||
network::CellularOfferedCandidate candidate;
|
||||
candidate.cost_energy_pj = 10;
|
||||
candidate.cost_matter_fg = {1, 2, 3, 4};
|
||||
network::CellularLifeDiagnostics reserve;
|
||||
reserve.reserve_energy_pj = 10;
|
||||
reserve.reserve_matter_fg = {1, 2, 3, 4};
|
||||
CHECK(can_fund(candidate, reserve));
|
||||
reserve.reserve_matter_fg[3] = 3;
|
||||
CHECK(!can_fund(candidate, reserve));
|
||||
reserve.reserve_matter_fg[3] = 4;
|
||||
reserve.reserve_energy_pj = 9;
|
||||
CHECK(!can_fund(candidate, reserve));
|
||||
std::vector<render::Rectangle> scene;
|
||||
patch_box(scene, {-10, 100}, {100, -10}, true, true, 80, 80);
|
||||
CHECK(scene.size() == patch_rectangles);
|
||||
for (const auto& r : scene)
|
||||
CHECK(r.x >= 0 && r.y >= 0 && r.x + r.width <= 80 && r.y + r.height <= 80);
|
||||
}
|
||||
void labels() {
|
||||
const auto c = cell(0);
|
||||
const auto water = describe_field(c, world::Field::water);
|
||||
@@ -332,6 +390,81 @@ void session_host() {
|
||||
CHECK(!host.connect(FakeSession{server}));
|
||||
CHECK(server.alive == 0 && host.session() == nullptr);
|
||||
}
|
||||
void cellular_play() {
|
||||
using namespace evo::client_app;
|
||||
using namespace evo::network;
|
||||
CellularPlay play;
|
||||
play.initialize(100);
|
||||
CHECK(play.birth_request() == 100 && play.birth_pending());
|
||||
play.connected();
|
||||
play.observe({101, 900, {}, CellularLifeDiagnostics{}});
|
||||
CHECK(play.can_act() && !play.can_restart());
|
||||
CellularLifeDiagnostics feedback;
|
||||
feedback.has_tick_effects = true;
|
||||
feedback.bud = CellularBudOutcome::insufficient_funding;
|
||||
play.observe({101, 900, {}, feedback});
|
||||
play.observe({102, 900, {}, CellularLifeDiagnostics{}});
|
||||
CHECK(play.bud_feedback() ==
|
||||
std::pair{std::uint64_t{101}, CellularBudOutcome::insufficient_funding});
|
||||
const CellularMutationOffer offer{101, 900, 1, 100, {{3, 2, 0, 50, 20, 30}}};
|
||||
play.observe_offer(offer);
|
||||
CHECK(!play.queue(CellAction::mutation, 102, {900, 2, 100, 3}));
|
||||
CHECK(!play.queue(CellAction::mutation, 100, {900, 1, 100, 3}));
|
||||
CHECK(!play.queue(CellAction::mutation, 102, {900, 1, 100, 8}));
|
||||
CHECK(play.queue(CellAction::mutation, 102, {900, 1, 100, 3}));
|
||||
const auto id = required(play.pending()).id;
|
||||
CHECK(!play.queue(CellAction::bud, 102)); // Double clicks cannot create another action.
|
||||
play.sent();
|
||||
play.receipt({id, AdmitStatus::not_ready, 101, 0});
|
||||
CHECK(required(play.pending()).state == CellActionState::unresolved);
|
||||
play.disconnected();
|
||||
CHECK(!play.can_act() && !play.offer());
|
||||
play.connected();
|
||||
CHECK(!play.can_act());
|
||||
play.observe({103, 900, {}, CellularLifeDiagnostics{}});
|
||||
play.receipt({id, AdmitStatus::invalid_action, 103, 0});
|
||||
CHECK(required(play.pending()).state ==
|
||||
CellActionState::unresolved); // Refused retry != failed original.
|
||||
play.outcome({id, ActionResult::committed, 102, 1});
|
||||
CHECK(!play.pending() && play.history().back().state == CellActionState::committed);
|
||||
CHECK(play.queue(CellAction::bud, 104));
|
||||
const auto bud = required(play.pending()).id;
|
||||
play.sent();
|
||||
play.observe_end({104, 900, CellularDeathReason::starved});
|
||||
play.observe({103, 900, {}, CellularLifeDiagnostics{}}); // Retained view is older than death.
|
||||
CHECK(play.life() == 0 && play.can_restart() && !play.can_act());
|
||||
CHECK(play.restart());
|
||||
const auto request = play.birth_request();
|
||||
CHECK(request != 100 && !play.restart());
|
||||
play.birth({100, PrimitiveLifeResult::born, 900, 1}); // Old request cannot finish this birth.
|
||||
CHECK(play.birth_pending());
|
||||
play.birth({request, PrimitiveLifeResult::born, 901, 3});
|
||||
CHECK(!play.can_restart()); // Birth reply may precede the first new-life view.
|
||||
play.observe({105, 901, {}, CellularLifeDiagnostics{}});
|
||||
play.observe_end({104, 900, CellularDeathReason::starved});
|
||||
CHECK(play.life() == 901 && !play.ended() && !play.pending());
|
||||
play.outcome({bud, ActionResult::not_committed, 0, 0});
|
||||
CHECK(play.history().back().state == CellActionState::refused);
|
||||
for (int i = 0; i < 20; ++i) {
|
||||
CHECK(play.queue(CellAction::engulf, 106));
|
||||
const auto action = required(play.pending()).id;
|
||||
play.sent();
|
||||
play.outcome({action, ActionResult::unknown, 0, 0});
|
||||
play.keep_unknown();
|
||||
}
|
||||
CHECK(play.history().size() == CellularPlay::history_limit);
|
||||
CHECK(play.history().back().state == CellActionState::unresolved);
|
||||
play.disconnected();
|
||||
CHECK(!play.queue(CellAction::engulf, 106) && !play.can_restart());
|
||||
play.connected();
|
||||
play.observe({106, 901, {}, CellularLifeDiagnostics{}});
|
||||
CHECK(play.queue(CellAction::bud, 107));
|
||||
play.disconnected();
|
||||
CHECK(!play.pending() && play.history().back().state == CellActionState::refused);
|
||||
CellularPlay exhausted;
|
||||
exhausted.initialize(std::numeric_limits<std::uint64_t>::max());
|
||||
CHECK(exhausted.allocate_id() == 0 && exhausted.allocate_id() == 0);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main() try {
|
||||
@@ -340,6 +473,8 @@ int main() try {
|
||||
launcher();
|
||||
outcome_log();
|
||||
session_host();
|
||||
cellular_play();
|
||||
cellular_assistance();
|
||||
std::puts("client UI model tests passed");
|
||||
return 0;
|
||||
} catch (const std::exception& e) {
|
||||
|
||||
Reference in New Issue
Block a user