Files
EVEngine/test/composable_gameplay.cpp
T

98 lines
5.2 KiB
C++

#include "zeroerr/assert.h"
#include "zeroerr/unittest.h"
#include "common/Module.h"
#include <simplesquirrel/simplesquirrel.hpp>
#include <fstream>
#include <sstream>
#include <string>
TEST_CASE("composableGameplay.scriptBuildsRebellionWithoutDomainCpp") {
const std::string path = std::string(EVENGINE_SOURCE_DIR) + "/examples/composable-rebellion/simulation.nut";
std::ifstream input(path);
REQUIRE(input.good());
std::ostringstream source;
source << input.rdbuf();
const std::string script = source.str();
ssq::VM vm(2048, ssq::Libs::ALL);
eve::ModuleManager::expose(vm);
vm.run(vm.compileSource(script.c_str()));
vm.run(vm.compileSource(R"(
scenarioResult <- run_rebellion_scenario();
scenarioOwner <- demoSocial.ownerOf(demoState["baseId"]);
scenarioProduction <- demoState.baseStats.getFinal("production_speed", 0.0);
scenarioOrder <- demoState.commandQueue.current().getKind();
scenarioCanCommand <- demoTags.hasCapability(demoState.general, "command_army");
scenarioEffect <- demoState.statusEffects.effectAt(0).getType();
scenarioEvent <- demoState.events.at(0).getType();
scenarioTransaction <- demoState.transactions.at(0).getState();
scenarioStateOwner <- demoState.authoritativeState.get(demoState.baseId, "owner");
scenarioRank <- demoState.definitions.resolve("rank", "rank.general").getJson();
scenarioAuthority <- demoState.authority.can(demoState.general, demoState.baseId, "govern_base");
scenarioPolicy <- demoState.policies.select("administration").getName();
scenarioProductionA <- demoState.production.taskAt(0).getState() + ":" +
demoState.production.taskAt(0).getKind();
scenarioProductionB <- demoState.production.taskAt(1).getState() + ":" +
demoState.production.taskAt(1).getKind();
scenarioAiTarget <- demoState.aiTarget;
scenarioAiState <- demoState.decisions.state("defense.ai");
scenarioAiAction <- demoState.aiAction;
scenarioAiVelocity <- demoState.aiVelocity;
scenarioAiThreat <- demoState.aiThreat;
)"));
CHECK(vm.find("scenarioResult").toBool());
CHECK_EQ(vm.find("scenarioOwner").toString(), std::string("faction.frontier"));
CHECK_EQ(vm.find("scenarioProduction").toFloat(), 1.4F);
CHECK_EQ(vm.find("scenarioOrder").toString(), std::string("secure_assets"));
CHECK(vm.find("scenarioCanCommand").toBool());
CHECK_EQ(vm.find("scenarioEffect").toString(), std::string("salary_unpaid"));
CHECK_EQ(vm.find("scenarioEvent").toString(), std::string("rebellion_started"));
CHECK_EQ(vm.find("scenarioTransaction").toString(), std::string("committed"));
CHECK_EQ(vm.find("scenarioStateOwner").toString(), std::string("\"faction.frontier\""));
CHECK_EQ(vm.find("scenarioRank").toString(), std::string("{\"authority\":[\"govern_base\",\"command_army\"],"
"\"commandCapacity\":8}"));
CHECK(vm.find("scenarioAuthority").toBool());
CHECK_EQ(vm.find("scenarioPolicy").toString(), std::string("governor_bonus"));
CHECK_EQ(vm.find("scenarioProductionA").toString(), std::string("completed:build_unit"));
CHECK_EQ(vm.find("scenarioProductionB").toString(), std::string("completed:issue_decree"));
CHECK_EQ(vm.find("scenarioAiTarget").toString(), std::string("army.raiders"));
CHECK_EQ(vm.find("scenarioAiState").toString(), std::string("engage"));
CHECK_EQ(vm.find("scenarioAiAction").toString(), std::string("attack"));
CHECK(vm.find("scenarioAiVelocity").toString().find("\"x\"") != std::string::npos);
CHECK_EQ(vm.find("scenarioAiThreat").toFloat(), 0.8F);
vm.run(vm.compileSource(R"(
reset_demo();
demoState.authority.revokeBySource("rank.general", "rank_removed");
refresh_governor_bonus();
deniedProduction <- demoState.baseStats.getFinal("production_speed", 0.0);
deniedReason <- demoState.authority.explain(
demoState.general, demoState.baseId, "govern_base").getReason();
)"));
CHECK_EQ(vm.find("deniedProduction").toFloat(), 1.0F);
CHECK_EQ(vm.find("deniedReason").toString(), std::string("no_matching_rule"));
vm.run(vm.compileSource(R"(
reset_demo();
apply_unpaid_salary();
local conflict = demoState.authoritativeState.newBatch();
conflict.setExpected(demoState.baseId, "owner", "\"faction.usurper\"", "\"faction.crown\"");
demoState.authoritativeState.commit(conflict);
conflictResult <- evaluate_rebellion();
conflictGeneralOwner <- demoState.authoritativeState.get(demoState.general, "owner");
conflictSocialOwner <- demoSocial.ownerOf(demoState.general);
conflictTransaction <- demoState.transactions.at(0).getState();
conflictEvents <- demoState.events.size();
)"));
CHECK(!vm.find("conflictResult").toBool());
CHECK_EQ(vm.find("conflictGeneralOwner").toString(), std::string("\"faction.crown\""));
CHECK_EQ(vm.find("conflictSocialOwner").toString(), std::string("faction.crown"));
CHECK_EQ(vm.find("conflictTransaction").toString(), std::string("failed"));
CHECK_EQ(vm.find("conflictEvents").toInt(), int64_t{0});
}