C++游戏-谁与争锋1.0(正式版)
cout << "钛矿: " << setw(10) << static_cast<int>(resources.titanium) << "能源: " << setw(10) << static_cast<int>(resources.energy) << " ║" << endl;cout << "信息: " << setw(2) << tech.information << "机器人: "
经过3个月的积累,终于,2324行的"谁与争锋"国家模拟游戏正式进入公测阶段。
话不多说,献上代码。
#include <bits/stdc++.h>
#include <random>
#include <memory>
#include <windows.h>
#include <iomanip>
#include <unordered_map>
#include <fstream>
#include <sstream>
#include <thread>
#include <chrono>
using namespace std;
// 颜色控制函数
void pr(const char* s, int color) {
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | color);
printf(s);
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | 7);
}
// 地形类型
enum class Terrain {
PLAINS, // 平原
MOUNTAINS, // 山脉
FOREST, // 森林
DESERT, // 沙漠
COASTAL, // 沿海
ARCTIC, // 极地
URBAN, // 城市
VOLCANIC // 火山
};
// 国家关系
enum class Relation {
ALLY, // 同盟
NEUTRAL, // 中立
ENEMY, // 敌国
VASSAL, // 附庸
TRADE_PARTNER // 贸易伙伴
};
// 科技时代
enum class TechEra {
STONE_AGE, // 石器时代
BRONZE_AGE, // 青铜时代
IRON_AGE, // 铁器时代
CLASSICAL, // 古典时代
MEDIEVAL, // 中世纪
RENAISSANCE, // 文艺复兴
INDUSTRIAL, // 工业时代
MODERN, // 现代
INFORMATION, // 信息时代
DIGITAL, // 数字时代
BIOTECH, // 生物科技时代
SPACE_AGE // 太空时代
};
// 详细的科技等级结构
struct Technology {
// 基础科技 (1-20)
int agriculture; // 农业科技
int military; // 军事科技
int economic; // 经济科技
int medical; // 医疗科技
int industrial; // 工业科技
int construction; // 建筑科技
// 高级科技分支 (0-20)
int electronics; // 电子科技
int aerospace; // 航空航天
int nuclear; // 核能科技
int biotechnology; // 生物技术
int information; // 信息技术
int robotics; // 机器人技术
int nanotechnology; // 纳米技术
int quantum; // 量子科技
// 特殊科技标志
bool has_advanced_medicine;
bool has_steel_production;
bool has_navigation;
bool has_electricity;
bool has_computers;
bool has_internet;
bool has_ai;
bool has_fusion_power;
bool has_teleportation;
bool has_time_travel;
Technology() :
agriculture(1), military(1), economic(1), medical(1), industrial(1), construction(1),
electronics(0), aerospace(0), nuclear(0), biotechnology(0), information(0),
robotics(0), nanotechnology(0), quantum(0),
has_advanced_medicine(false), has_steel_production(false),
has_navigation(false), has_electricity(false), has_computers(false),
has_internet(false), has_ai(false), has_fusion_power(false),
has_teleportation(false), has_time_travel(false) {}
// 获取当前科技时代
TechEra getCurrentEra() const {
double tech_score = (agriculture + military + economic + medical + industrial + construction +
electronics + aerospace + nuclear + biotechnology + information +
robotics + nanotechnology + quantum) / 14.0;
if (tech_score >= 18) return TechEra::SPACE_AGE;
if (tech_score >= 16) return TechEra::BIOTECH;
if (tech_score >= 14) return TechEra::DIGITAL;
if (tech_score >= 12) return TechEra::INFORMATION;
if (tech_score >= 10) return TechEra::MODERN;
if (tech_score >= 8) return TechEra::INDUSTRIAL;
if (tech_score >= 6) return TechEra::RENAISSANCE;
if (tech_score >= 4) return TechEra::MEDIEVAL;
if (tech_score >= 3) return TechEra::CLASSICAL;
if (tech_score >= 2) return TechEra::IRON_AGE;
if (tech_score >= 1.5) return TechEra::BRONZE_AGE;
return TechEra::STONE_AGE;
}
// 检查科技前提
bool canResearch(const string& tech_name) const {
if (tech_name == "electronics" && economic < 3) return false;
if (tech_name == "aerospace" && electronics < 2) return false;
if (tech_name == "nuclear" && industrial < 5) return false;
if (tech_name == "biotechnology" && medical < 4) return false;
if (tech_name == "information" && electronics < 4) return false;
if (tech_name == "robotics" && information < 3) return false;
if (tech_name == "nanotechnology" && robotics < 4) return false;
if (tech_name == "quantum" && nanotechnology < 5) return false;
return true;
}
};
// 建筑等级
struct Buildings {
int farms; // 农场
int mines; // 矿场
int lumber_mills; // 伐木场
int barracks; // 兵营
int universities; // 大学
int hospitals; // 医院
int factories; // 工厂
int power_plants; // 发电厂
int research_labs; // 研究所
int military_academy; // 军事学院
int airports; // 机场
int naval_bases; // 海军基地
int space_ports; // 太空港
int ai_cores; // AI核心
int quantum_computers; // 量子计算机
Buildings() :
farms(1), mines(1), lumber_mills(1), barracks(1),
universities(0), hospitals(0), factories(0),
power_plants(0), research_labs(0), military_academy(0),
airports(0), naval_bases(0), space_ports(0), ai_cores(0), quantum_computers(0) {}
};
// 军队单位结构
struct MilitaryUnit {
string name;
string type; // "infantry", "vehicle", "aircraft", "navy", "artillery", "special", "space"
int level; // 1-10级
int count;
double attack_power;
double defense_power;
double mobility;
double range;
double maintenance_cost;
vector<string> special_abilities;
MilitaryUnit(string n, string t, int lvl, double attack, double defense, double mob, double rng, double cost)
: name(n), type(t), level(lvl), count(0), attack_power(attack), defense_power(defense),
mobility(mob), range(rng), maintenance_cost(cost) {}
void addAbility(const string& ability) {
special_abilities.push_back(ability);
}
};
// 军事力量结构
class MilitaryForces {
private:
vector<MilitaryUnit> units;
public:
MilitaryForces() {
initializeUnits();
}
void initializeUnits() {
// 步兵单位 (1-10级)
units.emplace_back("部落战士", "infantry", 1, 15, 8, 2, 1, 1);
units.emplace_back("正规军", "infantry", 2, 30, 15, 3, 1, 2);
units.emplace_back("特种部队", "infantry", 3, 60, 30, 4, 2, 4);
units.emplace_back("未来战士", "infantry", 4, 120, 60, 6, 3, 8);
units.emplace_back("基因战士", "infantry", 5, 240, 120, 8, 4, 16);
units.emplace_back("纳米战士", "infantry", 6, 480, 240, 10, 6, 32);
units.emplace_back("能量战士", "infantry", 7, 960, 480, 12, 8, 64);
units.emplace_back("量子战士", "infantry", 8, 1920, 960, 15, 10, 128);
units.emplace_back("时空战士", "infantry", 9, 3840, 1920, 18, 12, 256);
units.emplace_back("神级战士", "infantry", 10, 7680, 3840, 20, 15, 512);
// 车辆单位
units.emplace_back("战车", "vehicle", 1, 40, 25, 4, 2, 6);
units.emplace_back("坦克", "vehicle", 2, 80, 50, 5, 3, 12);
units.emplace_back("主战坦克", "vehicle", 3, 160, 100, 6, 4, 24);
units.emplace_back("未来坦克", "vehicle", 4, 320, 200, 8, 6, 48);
units.emplace_back("机甲部队", "vehicle", 5, 640, 400, 10, 8, 96);
units.emplace_back("纳米机甲", "vehicle", 6, 1280, 800, 12, 10, 192);
units.emplace_back("量子坦克", "vehicle", 7, 2560, 1600, 14, 12, 384);
units.emplace_back("时空战车", "vehicle", 8, 5120, 3200, 16, 14, 768);
units.emplace_back("反物质坦克", "vehicle", 9, 10240, 6400, 18, 16, 1536);
units.emplace_back("神级战车", "vehicle", 10, 20480, 12800, 20, 18, 3072);
// 空军单位
units.emplace_back("战斗机", "aircraft", 1, 60, 25, 10, 8, 18);
units.emplace_back("喷气式战机", "aircraft", 2, 120, 50, 12, 10, 36);
units.emplace_back("隐形战机", "aircraft", 3, 240, 100, 15, 12, 72);
units.emplace_back("太空战机", "aircraft", 4, 480, 200, 20, 15, 144);
units.emplace_back("无人机群", "aircraft", 5, 960, 400, 25, 20, 288);
units.emplace_back("量子战机", "aircraft", 6, 1920, 800, 30, 25, 576);
units.emplace_back("时空战机", "aircraft", 7, 3840, 1600, 35, 30, 1152);
units.emplace_back("反物质战机", "aircraft", 8, 7680, 3200, 40, 35, 2304);
units.emplace_back("星际战机", "aircraft", 9, 15360, 6400, 45, 40, 4608);
units.emplace_back("神级战机", "aircraft", 10, 30720, 12800, 50, 45, 9216);
// 海军单位
units.emplace_back("护卫舰", "navy", 1, 50, 35, 6, 5, 14);
units.emplace_back("驱逐舰", "navy", 2, 100, 70, 7, 6, 28);
units.emplace_back("航空母舰", "navy", 3, 200, 140, 8, 10, 56);
units.emplace_back("核动力航母", "navy", 4, 400, 280, 9, 12, 112);
units.emplace_back("太空战舰", "navy", 5, 800, 560, 12, 15, 224);
units.emplace_back("量子战舰", "navy", 6, 1600, 1120, 14, 18, 448);
units.emplace_back("时空战舰", "navy", 7, 3200, 2240, 16, 20, 896);
units.emplace_back("反物质战舰", "navy", 8, 6400, 4480, 18, 22, 1792);
units.emplace_back("星际母舰", "navy", 9, 12800, 8960, 20, 25, 3584);
units.emplace_back("神级战舰", "navy", 10, 25600, 17920, 22, 28, 7168);
// 特殊单位
units.emplace_back("工程师", "special", 1, 10, 8, 4, 2, 3);
units.emplace_back("间谍", "special", 2, 15, 12, 6, 3, 5);
units.emplace_back("网络战部队", "special", 3, 25, 18, 5, 10, 12);
units.emplace_back("卫星武器", "special", 4, 50, 30, 0, 100, 60);
units.emplace_back("纳米机器人", "special", 5, 100, 60, 12, 8, 120);
units.emplace_back("量子特工", "special", 6, 200, 120, 15, 12, 240);
units.emplace_back("时空特工", "special", 7, 400, 240, 18, 15, 480);
units.emplace_back("AI军团", "special", 8, 800, 480, 20, 18, 960);
units.emplace_back("反物质部队", "special", 9, 1600, 960, 22, 20, 1920);
units.emplace_back("神级特工", "special", 10, 3200, 1920, 25, 22, 3840);
// 添加特殊能力
for (auto& unit : units) {
if (unit.level >= 5) unit.addAbility("高级装备");
if (unit.level >= 7) unit.addAbility("能量护盾");
if (unit.level >= 9) unit.addAbility("量子隐形");
if (unit.type == "special" && unit.level >= 3) unit.addAbility("情报收集");
if (unit.type == "aircraft" && unit.level >= 4) unit.addAbility("超音速");
if (unit.type == "navy" && unit.level >= 6) unit.addAbility("深海潜行");
}
}
MilitaryUnit* getUnit(const string& type, int level) {
for (auto& unit : units) {
if (unit.type == type && unit.level == level) {
return &unit;
}
}
return nullptr;
}
vector<MilitaryUnit*> getAvailableUnits(int max_tech_level) {
vector<MilitaryUnit*> available;
for (auto& unit : units) {
if (unit.level <= max_tech_level) {
available.push_back(&unit);
}
}
return available;
}
bool recruitUnit(const string& type, int level, int count) {
MilitaryUnit* unit = getUnit(type, level);
if (unit) {
unit->count += count;
return true;
}
return false;
}
bool upgradeUnit(const string& type, int from_level, int to_level, int count) {
MilitaryUnit* from_unit = getUnit(type, from_level);
MilitaryUnit* to_unit = getUnit(type, to_level);
if (from_unit && to_unit && from_unit->count >= count) {
from_unit->count -= count;
to_unit->count += count;
return true;
}
return false;
}
double totalPower() const {
double power = 0;
for (const auto& unit : units) {
power += unit.count * (unit.attack_power + unit.defense_power);
// 特殊能力加成
for (const auto& ability : unit.special_abilities) {
if (ability == "高级装备") power += unit.count * 10;
if (ability == "能量护盾") power += unit.count * 20;
if (ability == "量子隐形") power += unit.count * 30;
}
}
return power;
}
double totalMaintenance() const {
double cost = 0;
for (const auto& unit : units) {
cost += unit.count * unit.maintenance_cost;
}
return cost;
}
void displayForces() const {
pr("\n=== 军事力量详情 ===\n", 12);
map<string, vector<const MilitaryUnit*>> units_by_type;
for (const auto& unit : units) {
if (unit.count > 0) {
units_by_type[unit.type].push_back(&unit);
}
}
for (const auto& type_pair : units_by_type) {
const string& type = type_pair.first;
const vector<const MilitaryUnit*>& type_units = type_pair.second;
string type_name;
if (type == "infantry") type_name = "步兵";
else if (type == "vehicle") type_name = "车辆";
else if (type == "aircraft") type_name = "空军";
else if (type == "navy") type_name = "海军";
else if (type == "special") type_name = "特殊部队";
cout << "\n" << type_name << ":\n";
for (const auto unit : type_units) {
cout << " " << unit->name << " (等级 " << unit->level << "): " << unit->count
<< " 单位 | 攻击: " << unit->attack_power
<< " | 防御: " << unit->defense_power;
if (!unit->special_abilities.empty()) {
cout << " | 能力: ";
for (const auto& ability : unit->special_abilities) {
cout << ability << " ";
}
}
cout << endl;
}
}
cout << "\n总战力: " << totalPower() << endl;
cout << "总维护费用: " << totalMaintenance() << " 黄金/回合" << endl;
}
// 获取所有单位
const vector<MilitaryUnit>& getAllUnits() const {
return units;
}
// 设置单位数量(用于存档)
void setUnitCount(const string& type, int level, int count) {
for (auto& unit : units) {
if (unit.type == type && unit.level == level) {
unit.count = count;
return;
}
}
}
};
// 资源系统
struct Resources {
long long gold; // 黄金
long long food; // 食物
long long wood; // 木材
long long iron; // 铁矿石
long long oil; // 石油
long long uranium; // 铀矿
long long titanium; // 钛矿
long long antimatter; // 反物质
long long research; // 科研点数
long long energy; // 能源
long long dark_matter; // 暗物质
Resources() : gold(10000), food(20000), wood(10000), iron(5000), oil(0),
uranium(0), titanium(0), antimatter(0), research(0), energy(0), dark_matter(0) {}
};
// 全局随机数生成器
mt19937 global_rng(time(nullptr));
// 国家类
class Nation {
private:
string name;
string leader_name;
Terrain terrain;
Resources resources;
MilitaryForces military;
Technology tech;
Buildings buildings;
map<string, Relation> diplomacy;
// 国家属性
double economy; // 经济指数 0-200
double agriculture; // 农业指数 0-200
double health; // 健康指数 0-200
double stability; // 稳定度 0-200
double happiness; // 幸福度 0-200
double education; // 教育水平 0-200
int population; // 人口
int territory_size; // 领土大小
double inflation; // 通货膨胀率
// 升级相关
int turn_count;
double global_influence; // 全球影响力
public:
Nation(string n, string leader, Terrain t) :
name(n), leader_name(leader), terrain(t), economy(60),
agriculture(60), health(80), stability(75), happiness(70), education(50),
population(2000000), territory_size(2000), inflation(1.0), turn_count(0), global_influence(10) {}
// 获取国家名称
string getName() const { return name; }
string getLeaderName() const { return leader_name; }
// 获取地形
Terrain getTerrain() const { return terrain; }
// 获取军事力量
const MilitaryForces& getMilitary() const { return military; }
MilitaryForces& getMilitary() { return military; }
// 获取资源
const Resources& getResources() const { return resources; }
Resources& getResources() { return resources; }
// 获取科技
const Technology& getTechnology() const { return tech; }
Technology& getTechnology() { return tech; }
// 获取建筑
const Buildings& getBuildings() const { return buildings; }
Buildings& getBuildings() { return buildings; }
// 获取人口
int getPopulation() const { return population; }
// 获取经济指数
double getEconomy() const { return economy; }
// 获取全球影响力
double getGlobalInfluence() const { return global_influence; }
// 设置外交关系
void setRelation(const string& otherNation, Relation rel) {
diplomacy[otherNation] = rel;
}
// 获取外交关系
Relation getRelation(const string& otherNation) const {
auto it = diplomacy.find(otherNation);
if (it != diplomacy.end()) {
return it->second;
}
return Relation::NEUTRAL;
}
// 获取所有外交关系
const map<string, Relation>& getAllRelations() const {
return diplomacy;
}
// 经济发展
void developEconomy() {
long double cost = 1000 * (1 + tech.economic * 0.1);
if (resources.gold >= cost) {
resources.gold -= cost;
uniform_int_distribution<int> dist_econ(8, 22);
economy += dist_econ(global_rng) * (1 + tech.economic * 0.05);
resources.research += 15;
inflation += 0.01;
pr("经济发展成功!经济指数上升\n", 10);
} else {
pr("资金不足,无法发展经济\n", 12);
}
}
// 农业发展
void developAgriculture() {
long double cost = 2000 * (1 + tech.agriculture * 0.1);
if (resources.food >= cost * 0.5) {
resources.food -= cost * 0.5;
uniform_int_distribution<int> dist_agri(8, 19);
agriculture += dist_agri(global_rng) * (1 + tech.agriculture * 0.05);
population += 100000 * (1 + tech.agriculture * 0.1);
resources.research += 12;
pr("农业发展成功!农业指数上升,人口增长\n", 10);
} else {
pr("食物不足,无法发展农业\n", 12);
}
}
// 医疗发展
void developHealth() {
long double cost = 600 * (1 + tech.medical * 0.1);
if (resources.gold >= cost) {
resources.gold -= cost;
uniform_int_distribution<int> dist_health(6, 15);
health += dist_health(global_rng) * (1 + tech.medical * 0.05);
stability += 8;
happiness += 5;
population += 50000;
resources.research += 18;
pr("医疗发展成功!健康指数上升\n", 11);
} else {
pr("资金不足,无法发展医疗\n", 12);
}
}
// 工业发展
void developIndustry() {
double cost = 1000 * (1 + tech.industrial * 0.1);
if (resources.gold >= cost && resources.iron >= 800) {
resources.gold -= cost;
resources.iron -= 800;
economy += 12;
resources.research += 20;
pr("工业发展成功!经济指数上升\n", 13);
} else {
pr("资源不足,无法发展工业\n", 12);
}
}
// 教育发展
void developEducation() {
double cost = 500 * (1 + tech.economic * 0.1);
if (resources.gold >= cost) {
resources.gold -= cost;
uniform_int_distribution<int> dist_edu(5, 12);
education += dist_edu(global_rng) * (1 + tech.economic * 0.03);
resources.research += 25;
happiness +=5;
pr("教育发展成功!教育水平上升\n", 14);
} else {
pr("资金不足,无法发展教育\n", 12);
}
}
// 建筑升级系统
void upgradeBuilding(const string& building_type) {
double cost_multiplier = 1.0;
int* building_level = nullptr;
string building_name;
if (building_type == "farm") {
building_level = &buildings.farms;
building_name = "农场";
cost_multiplier = 100;
} else if (building_type == "mine") {
building_level = &buildings.mines;
building_name = "矿场";
cost_multiplier = 400;
} else if (building_type == "lumber") {
building_level = &buildings.lumber_mills;
building_name = "伐木场";
cost_multiplier = 350;
} else if (building_type == "barracks") {
building_level = &buildings.barracks;
building_name = "兵营";
cost_multiplier = 500;
} else if (building_type == "university") {
building_level = &buildings.universities;
building_name = "大学";
cost_multiplier = 800;
} else if (building_type == "hospital") {
building_level = &buildings.hospitals;
building_name = "医院";
cost_multiplier = 700;
} else if (building_type == "factory") {
building_level = &buildings.factories;
building_name = "工厂";
cost_multiplier = 800;
} else if (building_type == "power_plant") {
building_level = &buildings.power_plants;
building_name = "发电厂";
cost_multiplier = 800;
} else if (building_type == "research_lab") {
building_level = &buildings.research_labs;
building_name = "研究所";
cost_multiplier = 1000;
} else if (building_type == "military_academy") {
building_level = &buildings.military_academy;
building_name = "军事学院";
cost_multiplier = 1000;
} else if (building_type == "airport") {
building_level = &buildings.airports;
building_name = "机场";
cost_multiplier = 1500;
} else if (building_type == "naval_base") {
building_level = &buildings.naval_bases;
building_name = "海军基地";
cost_multiplier = 1400;
} else if (building_type == "space_port") {
building_level = &buildings.space_ports;
building_name = "太空港";
cost_multiplier = 5000;
} else if (building_type == "ai_core") {
building_level = &buildings.ai_cores;
building_name = "AI核心";
cost_multiplier = 3000;
} else if (building_type == "quantum_computer") {
building_level = &buildings.quantum_computers;
building_name = "量子计算机";
cost_multiplier = 4000;
}
if (building_level) {
double cost = cost_multiplier * (*building_level);
if (resources.gold >= cost) {
resources.gold -= cost;
(*building_level)++;
pr((building_name + "升级成功!当前等级: " + to_string(*building_level) + "\n").c_str(), 10);
} else {
pr("资金不足,无法升级建筑\n", 12);
}
}
}
// 科技研究系统
void researchTechnology(const string& tech_type) {
double research_cost = 0;
int* tech_level = nullptr;
string tech_name;
int max_level = 20;
if (tech_type == "agriculture") {
tech_level = &tech.agriculture;
tech_name = "农业科技";
research_cost = 1200;
} else if (tech_type == "military") {
tech_level = &tech.military;
tech_name = "军事科技";
research_cost = 1800;
} else if (tech_type == "economic") {
tech_level = &tech.economic;
tech_name = "经济科技";
research_cost = 1500;
} else if (tech_type == "medical") {
tech_level = &tech.medical;
tech_name = "医疗科技";
research_cost = 1400;
} else if (tech_type == "industrial") {
tech_level = &tech.industrial;
tech_name = "工业科技";
research_cost = 2000;
} else if (tech_type == "construction") {
tech_level = &tech.construction;
tech_name = "建筑科技";
research_cost = 1300;
} else if (tech_type == "electronics") {
tech_level = &tech.electronics;
tech_name = "电子科技";
research_cost = 2500;
} else if (tech_type == "aerospace") {
tech_level = &tech.aerospace;
tech_name = "航空航天";
research_cost = 3000;
} else if (tech_type == "nuclear") {
tech_level = &tech.nuclear;
tech_name = "核能科技";
research_cost = 3500;
} else if (tech_type == "biotechnology") {
tech_level = &tech.biotechnology;
tech_name = "生物技术";
research_cost = 2800;
} else if (tech_type == "information") {
tech_level = &tech.information;
tech_name = "信息技术";
research_cost = 2200;
} else if (tech_type == "robotics") {
tech_level = &tech.robotics;
tech_name = "机器人技术";
research_cost = 3200;
} else if (tech_type == "nanotechnology") {
tech_level = &tech.nanotechnology;
tech_name = "纳米技术";
research_cost = 4000;
} else if (tech_type == "quantum") {
tech_level = &tech.quantum;
tech_name = "量子科技";
research_cost = 5000;
}
if (tech_level && *tech_level < max_level) {
if (!tech.canResearch(tech_type)) {
pr("科技前提条件未满足,无法研究该科技\n", 12);
return;
}
double actual_cost = research_cost * (*tech_level + 1);
if (resources.research >= actual_cost) {
resources.research -= actual_cost;
(*tech_level)++;
unlockSpecialTechnologies();
pr((tech_name + "研究成功!当前等级: " + to_string(*tech_level) + "\n").c_str(), 14);
TechEra new_era = tech.getCurrentEra();
if (new_era != getPreviousEra()) {
pr(("科技时代升级!当前时代: " + getEraName(new_era) + "\n").c_str(), 13);
}
} else {
pr("科研点数不足,无法研究科技\n", 12);
}
} else if (tech_level && *tech_level >= max_level) {
pr((tech_name + "已达到最大等级\n").c_str(), 13);
} else {
pr("无效的科技类型\n", 12);
}
}
// 解锁特殊科技
void unlockSpecialTechnologies() {
if (tech.medical >= 5 && !tech.has_advanced_medicine) {
tech.has_advanced_medicine = true;
pr("解锁高级医疗技术!医院效果提升50%\n", 11);
}
if (tech.industrial >= 4 && !tech.has_steel_production) {
tech.has_steel_production = true;
pr("解锁钢铁生产技术!工业效率提升30%\n", 13);
}
if (tech.electronics >= 3 && !tech.has_electricity) {
tech.has_electricity = true;
pr("解锁电力技术!能源生产翻倍\n", 10);
}
if (tech.information >= 5 && !tech.has_computers) {
tech.has_computers = true;
pr("解锁计算机技术!科研效率提升100%\n", 14);
}
if (tech.information >= 8 && !tech.has_internet) {
tech.has_internet = true;
pr("解锁互联网技术!经济和科研全面增强\n", 14);
}
if (tech.robotics >= 6 && !tech.has_ai) {
tech.has_ai = true;
pr("解锁人工智能技术!自动化生产实现\n", 13);
}
if (tech.nuclear >= 8 && !tech.has_fusion_power) {
tech.has_fusion_power = true;
pr("解锁核聚变技术!能源问题彻底解决\n", 11);
}
if (tech.quantum >= 10 && !tech.has_teleportation) {
tech.has_teleportation = true;
pr("解锁量子传送技术!军事机动性大幅提升\n", 13);
}
if (tech.quantum >= 15 && !tech.has_time_travel) {
tech.has_time_travel = true;
pr("解锁时间旅行技术!获得终极力量\n", 11);
}
}
TechEra getPreviousEra() const {
Technology temp_tech = tech;
if (temp_tech.agriculture > 1) temp_tech.agriculture--;
if (temp_tech.military > 1) temp_tech.military--;
if (temp_tech.economic > 1) temp_tech.economic--;
return temp_tech.getCurrentEra();
}
string getEraName(TechEra era) const {
switch(era) {
case TechEra::STONE_AGE: return "石器时代";
case TechEra::BRONZE_AGE: return "青铜时代";
case TechEra::IRON_AGE: return "铁器时代";
case TechEra::CLASSICAL: return "古典时代";
case TechEra::MEDIEVAL: return "中世纪";
case TechEra::RENAISSANCE: return "文艺复兴";
case TechEra::INDUSTRIAL: return "工业时代";
case TechEra::MODERN: return "现代";
case TechEra::INFORMATION: return "信息时代";
case TechEra::DIGITAL: return "数字时代";
case TechEra::BIOTECH: return "生物科技时代";
case TechEra::SPACE_AGE: return "太空时代";
default: return "未知时代";
}
}
// 招募军队
void recruitMilitary(string type, int level, int count) {
MilitaryUnit* unit = military.getUnit(type, level);
if (!unit) {
pr("无效的兵种类型或等级\n", 12);
return;
}
if (level > tech.military) {
pr("军事科技等级不足,无法招募该等级单位\n", 12);
return;
}
// 特殊建筑要求
if (type == "aircraft" && level >= 3 && buildings.airports < level - 2) {
pr("机场等级不足,无法招募该空军单位\n", 12);
return;
}
if (type == "navy" && level >= 3 && buildings.naval_bases < level - 2) {
pr("海军基地等级不足,无法招募该海军单位\n", 12);
return;
}
if (type == "special" && level >= 7 && buildings.ai_cores < 1) {
pr("需要AI核心才能招募高级特殊部队\n", 12);
return;
}
// 计算成本
double cost_multiplier = 1.0;
if (type == "infantry") cost_multiplier = 15;
else if (type == "vehicle") cost_multiplier = 30;
else if (type == "aircraft") cost_multiplier = 100;
else if (type == "navy") cost_multiplier = 80;
else if (type == "artillery") cost_multiplier = 50;
else if (type == "special") cost_multiplier = 40;
double cost = count * cost_multiplier * level * inflation;
double iron_cost = count * level * 3;
double oil_cost = (type == "vehicle" || type == "aircraft" || type == "navy") ? count * level * 2 : 0;
double titanium_cost = (level >= 6) ? count * level : 0;
if (resources.gold >= cost && resources.iron >= iron_cost && resources.oil >= oil_cost && resources.titanium >= titanium_cost) {
military.recruitUnit(type, level, count);
resources.gold -= cost;
resources.iron -= iron_cost;
resources.oil -= oil_cost;
resources.titanium -= titanium_cost;
pr(("成功招募 " + to_string(count) + " 个 " + unit->name + "\n").c_str(), 10);
} else {
pr("资源不足,无法招募部队\n", 12);
}
}
// 资源生产
void produceResources() {
// 基础产出
double gold_income = economy * 200 * (1 + tech.economic * 0.1) * (1 + buildings.factories * 0.05);
double food_production = agriculture * 300 * (1 + tech.agriculture * 0.1) * (1 + buildings.farms * 0.08);
double wood_production = 400 * (1 + buildings.lumber_mills * 0.1);
double iron_production = 200 * (1 + buildings.mines * 0.1);
double oil_production = max(0, tech.industrial - 2) * 100;
double uranium_production = (tech.nuclear >= 1) ? tech.nuclear * 40 : 0;
double titanium_production = (tech.aerospace >= 3) ? tech.aerospace * 30 : 0;
double antimatter_production = (tech.quantum >= 12) ? tech.quantum * 10 : 0;
double research_production = 100 * (1 + buildings.universities * 0.15 + buildings.research_labs * 0.2 + buildings.ai_cores * 0.5);
double energy_production = buildings.power_plants * 200;
double dark_matter_production = (tech.quantum >= 18) ? 5 : 0;
// 科技加成
if (tech.has_electricity) {
gold_income *= 1.3;
research_production *= 1.4;
}
if (tech.has_computers) {
research_production *= 1.8;
gold_income *= 1.5;
}
if (tech.has_internet) {
gold_income *= 1.7;
research_production *= 2.0;
}
if (tech.has_ai) {
gold_income *= 2.0;
research_production *= 3.0;
}
if (tech.has_fusion_power) {
energy_production *= 5.0;
}
// 地形加成
if (terrain == Terrain::FOREST) wood_production *= 2.0;
if (terrain == Terrain::MOUNTAINS) {
iron_production *= 2.5;
titanium_production *= 1.5;
}
if (terrain == Terrain::DESERT) oil_production *= 3.0;
if (terrain == Terrain::PLAINS) food_production *= 1.8;
if (terrain == Terrain::COASTAL) gold_income *= 1.5;
if (terrain == Terrain::URBAN) {
gold_income *= 2.0;
research_production *= 1.8;
}
if (terrain == Terrain::VOLCANIC) {
uranium_production *= 2.0;
energy_production *= 1.5;
}
resources.gold += gold_income;
resources.food += food_production;
resources.wood += wood_production;
resources.iron += iron_production;
resources.oil += oil_production;
resources.uranium += uranium_production;
resources.titanium += titanium_production;
resources.antimatter += antimatter_production;
resources.research += research_production;
resources.energy += energy_production;
resources.dark_matter += dark_matter_production;
// 通货膨胀调整
resources.gold *= (1 - inflation * 0.01);
// 人口消耗和增长
double food_consumption = population * 0.002;
double energy_consumption = population * 0.001 + military.totalPower() * 0.02;
resources.food -= food_consumption;
resources.energy -= energy_consumption;
// 人口增长
if (resources.food > 0 && health > 60 && resources.energy > 0) {
double growth_rate = min(health / 150.0, resources.food / (population * 0.005));
growth_rate *= (1 + tech.medical * 0.08);
population += static_cast<int>(population * growth_rate * 0.015);
} else if (resources.food < 0 || health < 40 || resources.energy < 0) {
double decline_rate = 0.08;
population -= static_cast<int>(population * decline_rate);
stability -= 15;
happiness -= 20;
}
// 建筑维护费用
double maintenance_cost = (buildings.farms + buildings.mines + buildings.lumber_mills +
buildings.barracks + buildings.universities + buildings.hospitals +
buildings.factories + buildings.power_plants + buildings.research_labs +
buildings.military_academy + buildings.airports + buildings.naval_bases +
buildings.space_ports + buildings.ai_cores + buildings.quantum_computers) * 100;
resources.gold -= maintenance_cost;
// 军队维护费用
double military_maintenance = military.totalMaintenance() * inflation;
resources.gold -= military_maintenance;
// 全球影响力增长
global_influence += (economy + military.totalPower() / 10000 + tech.military) * 0.001;
// 幸福度和稳定度更新
updateHappinessAndStability();
turn_count++;
}
// 更新幸福度和稳定度
void updateHappinessAndStability() {
double base_happiness = 60.0;
double base_stability = 65.0;
// 食物影响
double food_ratio = resources.food / (population * 0.005);
base_happiness += min(30.0, food_ratio * 15);
base_stability += min(25.0, food_ratio * 10);
// 健康影响
base_happiness += (health - 60) * 0.4;
base_stability += (health - 60) * 0.3;
// 经济影响
base_happiness += (economy - 60) * 0.3;
base_stability += (economy - 60) * 0.4;
// 教育影响
base_happiness += (education - 50) * 0.2;
base_stability += (education - 50) * 0.2;
// 能源影响
if (resources.energy > 0) {
base_happiness += 15;
} else {
base_happiness -= 25;
base_stability -= 20;
}
// 通货膨胀影响
base_happiness -= inflation * 10;
base_stability -= inflation * 8;
// 军事压力
double military_pressure = military.totalPower() / 50000.0;
base_happiness -= min(25.0, military_pressure);
// 建筑提供幸福度
base_happiness += buildings.hospitals * 5;
base_happiness += buildings.universities * 8;
base_happiness += buildings.power_plants * 7.5;
happiness = max(0.0, min(200.0, base_happiness));
stability = max(0.0, min(200.0, base_stability));
}
// 显示详细状态
void displayStatus() const {
TechEra current_era = tech.getCurrentEra();
cout << "\n╔════════════════════════════════════════════════════════════╗" << endl;
cout << "║ " << name << " - " << leader_name << " ║" << endl;
cout << "║ " << getEraName(current_era) << " ║" << endl;
cout << "╠════════════════════════════════════════════════════════════╣" << endl;
// 基础信息
cout << "║ 地形: ";
switch(terrain) {
case Terrain::PLAINS: cout << "平原 "; break;
case Terrain::MOUNTAINS: cout << "山脉 "; break;
case Terrain::FOREST: cout << "森林 "; break;
case Terrain::DESERT: cout << "沙漠 "; break;
case Terrain::COASTAL: cout << "沿海 "; break;
case Terrain::ARCTIC: cout << "极地 "; break;
case Terrain::URBAN: cout << "城市 "; break;
case Terrain::VOLCANIC: cout << "火山 "; break;
}
cout << "人口: " << setw(15) << population << " ║" << endl;
// 国家指标
cout << "║ 经济: " << setw(5) << static_cast<int>(economy) << " 农业: " << setw(5) << static_cast<int>(agriculture);
cout << " 健康: " << setw(5) << static_cast<int>(health) << " 稳定: " << setw(5) << static_cast<int>(stability) << " ║" << endl;
cout << "║ 幸福: " << setw(5) << static_cast<int>(happiness) << " 教育: " << setw(5) << static_cast<int>(education);
cout << " 通胀: " << fixed << setprecision(1) << inflation << "% 影响力: " << setw(6) << static_cast<int>(global_influence) << " ║" << endl;
cout << "║ 领土: " << setw(6) << territory_size << " km2 回合: " << setw(4) << turn_count;
cout << " 战力: " << setw(12) << static_cast<int>(military.totalPower()) << " ║" << endl;
cout << "╠════════════════════════════════════════════════════════════╣" << endl;
cout << "║ 资源储备 ║" << endl;
cout << "║ 黄金: " << setw(10) << static_cast<int>(resources.gold) << " 食物: " << setw(10) << static_cast<int>(resources.food);
cout << " 木材: " << setw(10) << static_cast<int>(resources.wood) << " 铁矿: " << setw(10) << static_cast<int>(resources.iron) << " ║" << endl;
cout << "║ 石油: " << setw(10) << static_cast<int>(resources.oil) << " 铀矿: " << setw(10) << static_cast<int>(resources.uranium);
cout << " 钛矿: " << setw(10) << static_cast<int>(resources.titanium) << " 能源: " << setw(10) << static_cast<int>(resources.energy) << " ║" << endl;
cout << "║ 反物质: " << setw(8) << static_cast<int>(resources.antimatter) << " 暗物质: " << setw(8) << static_cast<int>(resources.dark_matter);
cout << " 科研: " << setw(10) << static_cast<int>(resources.research) << " ║" << endl;
cout << "╠════════════════════════════════════════════════════════════╣" << endl;
cout << "║ 科技水平 ║" << endl;
cout << "║ 农业: " << setw(2) << tech.agriculture << " 军事: " << setw(2) << tech.military;
cout << " 经济: " << setw(2) << tech.economic << " 医疗: " << setw(2) << tech.medical;
cout << " 工业: " << setw(2) << tech.industrial << " 建筑: " << setw(2) << tech.construction << " ║" << endl;
cout << "║ 电子: " << setw(2) << tech.electronics << " 航天: " << setw(2) << tech.aerospace;
cout << " 核能: " << setw(2) << tech.nuclear << " 生物: " << setw(2) << tech.biotechnology;
cout << " 信息: " << setw(2) << tech.information << " 机器人: " << setw(2) << tech.robotics << " ║" << endl;
cout << "║ 纳米: " << setw(2) << tech.nanotechnology << " 量子: " << setw(2) << tech.quantum;
cout << " ";
if (tech.has_time_travel) cout << "时间旅行";
else if (tech.has_teleportation) cout << "量子传送";
else if (tech.has_fusion_power) cout << "核聚变";
else if (tech.has_ai) cout << "人工智能";
else cout << " ";
cout << " ║" << endl;
cout << "╚════════════════════════════════════════════════════════════╝" << endl;
}
// 显示外交关系
void displayDiplomacy() const {
pr("\n=== 外交关系 ===\n", 11);
for (const auto& relation : diplomacy) {
cout << relation.first << ": ";
switch(relation.second) {
case Relation::ALLY: pr("同盟\n", 10); break;
case Relation::NEUTRAL: pr("中立\n", 7); break;
case Relation::ENEMY: pr("敌国\n", 12); break;
case Relation::VASSAL: pr("附庸\n", 13); break;
case Relation::TRADE_PARTNER: pr("贸易伙伴\n", 14); break;
}
}
}
// 检查是否灭亡
bool isDefeated() const {
return stability <= 0 || population <= 200000 || health <= 0 || happiness <= 0;
}
// 获取回合数
int getTurnCount() const { return turn_count; }
// 获取当前科技时代
TechEra getTechEra() const {
return tech.getCurrentEra();
}
// 为存档系统添加的getter和setter方法
double getAgriculture() const { return agriculture; }
double getHealth() const { return health; }
double getStability() const { return stability; }
double getHappiness() const { return happiness; }
double getEducation() const { return education; }
int getTerritorySize() const { return territory_size; }
double getInflation() const { return inflation; }
void setAgriculture(double value) { agriculture = value; }
void setHealth(double value) { health = value; }
void setStability(double value) { stability = value; }
void setHappiness(double value) { happiness = value; }
void setEducation(double value) { education = value; }
void setPopulation(int value) { population = value; }
void setTerritorySize(int value) { territory_size = value; }
void setInflation(double value) { inflation = value; }
void setTurnCount(int value) { turn_count = value; }
void setGlobalInfluence(double value) { global_influence = value; }
// 获取军事单位的完整信息(用于存档)
const vector<MilitaryUnit>& getMilitaryUnits() const {
return military.getAllUnits();
}
// 设置军事单位数量(用于读档)
void setMilitaryUnitCount(const string& type, int level, int count) {
military.setUnitCount(type, level, count);
}
};
// 战斗系统
class BattleSystem {
public:
static double getTerrainBonus(Terrain attacker, Terrain defender, const MilitaryForces& attacker_mil) {
double bonus = 1.0;
if (attacker == Terrain::MOUNTAINS && defender == Terrain::PLAINS) bonus *= 1.4;
if (attacker == Terrain::PLAINS && defender == Terrain::FOREST) bonus *= 0.7;
if (attacker == Terrain::FOREST && defender == Terrain::PLAINS) bonus *= 1.3;
if (attacker == Terrain::DESERT && defender == Terrain::COASTAL) bonus *= 1.2;
if (attacker == Terrain::COASTAL && defender != Terrain::COASTAL) bonus *= 1.2;
if (attacker == Terrain::URBAN) bonus *= 1.3;
if (attacker == Terrain::VOLCANIC) bonus *= 1.1;
return bonus;
}
static bool conductBattle(Nation& attacker, Nation& defender) {
pr(("\n" + attacker.getName() + " 向 " + defender.getName() + " 宣战!\n").c_str(), 12);
double attackerPower = attacker.getMilitary().totalPower();
double defenderPower = defender.getMilitary().totalPower();
double terrainBonus = getTerrainBonus(attacker.getTerrain(), defender.getTerrain(), attacker.getMilitary());
attackerPower *= terrainBonus;
double techBonus = 1.0 + (attacker.getTechnology().military - defender.getTechnology().military) * 0.15;
attackerPower *= techBonus;
uniform_real_distribution<double> dist(0.7, 1.3);
attackerPower *= dist(global_rng);
defenderPower *= dist(global_rng);
cout << "攻击方战力: " << attackerPower << " (地形加成: " << fixed << setprecision(1) << terrainBonus;
cout << ", 科技加成: " << techBonus << ")" << endl;
cout << "防御方战力: " << defenderPower << endl;
bool attackerWins = attackerPower > defenderPower;
if (attackerWins) {
pr("攻击方获得胜利!\n", 10);
// 胜利奖励
double dels=1.0/((rand()%50)*1.0+0.01);
attacker.getResources().gold += defender.getResources().gold*dels;
attacker.getResources().food += 6000;
attacker.getResources().iron += 1000;
attacker.getResources().oil += defender.getResources().oil*0.1;
attacker.getResources().research += 1000;
// 失败惩罚
defender.getResources().gold *=(1-dels);
defender.getResources().food *= 0.6;
defender.getResources().iron *= 0.6;
defender.getResources().oil*=0.9;
} else {
pr("防御方获得胜利!\n", 11);
double dels=1.0/((rand()%50)*1.0);
// 防御方获得奖励
defender.getResources().gold += attacker.getResources().gold*dels;
defender.getResources().iron += 400;
defender.getResources().research += 500;
attacker.getResources().gold*=0.98;
}
return attackerWins;
}
};
// 全球广播系统
class GlobalBroadcast {
public:
static void broadcastWorldStatus(const vector<shared_ptr<Nation>>& nations, int turn) {
pr("\n════════════════════════════ 全球局势报告 - 第 ", 14);
cout << turn << " 回合 ";
pr("════════════════════════════\n", 14);
cout << left << setw(20) << "国家" << setw(15) << "时代"
<< setw(12) << "人口" << setw(12) << "经济" << setw(12) << "军事"
<< setw(12) << "科技" << setw(15) << "全球影响力" << setw(10) << "状态" << endl;
vector<tuple<double, string, TechEra, int, double, double, int, double>> nation_data;
for (const auto& nation : nations) {
if (!nation->isDefeated()) {
double score = nation->getEconomy() + nation->getMilitary().totalPower() / 10000 +
nation->getGlobalInfluence() + nation->getPopulation() / 100000.0;
nation_data.emplace_back(score, nation->getName(), nation->getTechEra(),
nation->getPopulation(), nation->getEconomy(),
nation->getMilitary().totalPower(), nation->getTechnology().military,
nation->getGlobalInfluence());
}
}
// 按分数排序
sort(nation_data.rbegin(), nation_data.rend());
for (const auto& data : nation_data) {
double score;
string name;
TechEra era;
int pop;
double economy, military;
int tech;
double influence;
tie(score, name, era, pop, economy, tech, military, influence) = data;
cout << left << setw(20) << name
<< setw(15) << getEraName(era)
<< setw(12) << (pop > 1000000 ? to_string(pop/1000000) + "M" : to_string(pop/1000) + "K")
<< setw(12) << static_cast<int>(economy)
<< setw(12) << (military > 1000000 ? to_string(static_cast<int>(military)/1000000) + "M" :
military > 1000 ? to_string(static_cast<int>(military)/1000) + "K" : to_string(static_cast<int>(military)))
<< setw(12) << tech
<< setw(15) << fixed << setprecision(1) << influence
<< setw(10) << "繁荣" << endl;
}
// 显示灭亡国家
bool has_defeated = false;
for (const auto& nation : nations) {
if (nation->isDefeated()) {
if (!has_defeated) {
pr("\n已灭亡国家:\n", 12);
has_defeated = true;
}
cout << " " << nation->getName() << endl;
}
}
pr("\n══════════════════════════════════════════════════════════════════════════════════\n", 14);
}
private:
static string getEraName(TechEra era) {
switch(era) {
case TechEra::STONE_AGE: return "石器时代";
case TechEra::BRONZE_AGE: return "青铜时代";
case TechEra::IRON_AGE: return "铁器时代";
case TechEra::CLASSICAL: return "古典时代";
case TechEra::MEDIEVAL: return "中世纪";
case TechEra::RENAISSANCE: return "文艺复兴";
case TechEra::INDUSTRIAL: return "工业时代";
case TechEra::MODERN: return "现代";
case TechEra::INFORMATION: return "信息时代";
case TechEra::DIGITAL: return "数字时代";
case TechEra::BIOTECH: return "生物科技";
case TechEra::SPACE_AGE: return "太空时代";
default: return "未知";
}
}
};
// 增强的智能AI系统
class AIStrategy {
private:
shared_ptr<Nation> nation;
int aggression_level; // 0-10, 侵略性等级
int focus_type; // 0:平衡, 1:军事, 2:经济, 3:科技
public:
AIStrategy(shared_ptr<Nation> n) : nation(n) {
// 随机生成AI性格
uniform_int_distribution<int> agg_dist(0, 10);
uniform_int_distribution<int> focus_dist(0, 3);
aggression_level = agg_dist(global_rng);
focus_type = focus_dist(global_rng);
}
void executeTurn() {
TechEra current_era = nation->getTechEra();
double military_power = nation->getMilitary().totalPower();
double gold = nation->getResources().gold;
double research = nation->getResources().research;
double food = nation->getResources().food;
// 紧急情况处理
if (handleEmergencies()) {
return;
}
// 根据AI性格和当前状况制定策略
if (current_era <= TechEra::MEDIEVAL) {
executeAncientStrategy();
} else if (current_era <= TechEra::INDUSTRIAL) {
executeIndustrialStrategy();
} else if (current_era <= TechEra::INFORMATION) {
executeModernStrategy();
} else {
executeFutureStrategy();
}
// 根据侵略性决定是否宣战
if (shouldDeclareWar()) {
declareWar();
}
}
private:
bool handleEmergencies() {
// 处理紧急情况
if (nation->getResources().food < 5000) {
nation->developAgriculture();
return true;
}
if (nation->getResources().gold < 2000) {
nation->developEconomy();
return true;
}
if (nation->getStability() < 40) {
if (nation->getHealth() < 60) {
nation->developHealth();
} else {
nation->developEconomy();
}
return true;
}
return false;
}
void executeAncientStrategy() {
// 古代策略:重点发展基础资源
double priority_food = (nation->getResources().food < 15000) ? 2.0 : 0.5;
double priority_gold = (nation->getResources().gold < 8000) ? 1.5 : 0.7;
double priority_military = (aggression_level > 7) ? 1.8 : 0.8;
vector<pair<double, string>> actions = {
{priority_food, "agriculture"},
{priority_gold, "economy"},
{priority_military, "military"},
{1.0, "industry"},
{0.8, "health"}
};
// 根据优先级排序
sort(actions.rbegin(), actions.rend());
// 执行最高优先级行动
string best_action = actions[0].second;
executeAction(best_action);
}
void executeIndustrialStrategy() {
// 工业时代策略:平衡发展
vector<pair<double, string>> actions;
// 根据AI专注类型调整权重
double military_weight = (focus_type == 1) ? 2.0 : 1.0;
double economy_weight = (focus_type == 2) ? 2.0 : 1.0;
double tech_weight = (focus_type == 3) ? 2.0 : 1.0;
actions.emplace_back(military_weight * evaluateMilitaryNeed(), "military");
actions.emplace_back(economy_weight * evaluateEconomyNeed(), "economy");
actions.emplace_back(tech_weight * evaluateTechNeed(), getBestTechToResearch());
actions.emplace_back(1.0, "industry");
actions.emplace_back(0.8, "agriculture");
sort(actions.rbegin(), actions.rend());
executeAction(actions[0].second);
}
void executeModernStrategy() {
// 现代策略:科技优先,但考虑军事平衡
vector<pair<double, string>> actions;
double military_need = evaluateMilitaryNeed();
if (military_need > 0.7) {
actions.emplace_back(2.0, "military");
}
actions.emplace_back(1.5, getBestTechToResearch());
actions.emplace_back(1.2, "economy");
actions.emplace_back(1.0, "industry");
sort(actions.rbegin(), actions.rend());
executeAction(actions[0].second);
}
void executeFutureStrategy() {
// 未来策略:追求终极科技,但保持军事优势
vector<pair<double, string>> actions;
// 检查是否需要加强军事
if (evaluateMilitaryNeed() > 0.6) {
actions.emplace_back(1.8, "military");
}
// 优先研究高级科技
actions.emplace_back(2.0, getBestFutureTech());
actions.emplace_back(1.3, "economy");
sort(actions.rbegin(), actions.rend());
executeAction(actions[0].second);
}
double evaluateMilitaryNeed() {
// 评估军事需求
double current_power = nation->getMilitary().totalPower();
double expected_threat = aggression_level * 50000;
// 检查邻国军事力量
double neighbor_threat = 0;
// 这里可以添加检查邻国军事力量的逻辑
double total_threat = expected_threat + neighbor_threat;
if (current_power < total_threat * 0.7) {
return 1.0; // 急需加强军事
} else if (current_power < total_threat) {
return 0.6; // 需要加强军事
} else {
return 0.3; // 军事足够
}
}
double evaluateEconomyNeed() {
double gold = nation->getResources().gold;
if (gold < 5000) return 1.0;
if (gold < 15000) return 0.7;
return 0.3;
}
double evaluateTechNeed() {
TechEra era = nation->getTechEra();
if (era <= TechEra::MEDIEVAL) return 0.8;
if (era <= TechEra::INDUSTRIAL) return 1.0;
if (era <= TechEra::MODERN) return 1.2;
return 1.5;
}
string getBestTechToResearch() {
TechEra era = nation->getTechEra();
const Technology& tech = nation->getTechnology();
if (era <= TechEra::MEDIEVAL) {
if (tech.agriculture < 4) return "agriculture";
if (tech.military < 3) return "military";
return "economic";
} else if (era <= TechEra::INDUSTRIAL) {
if (tech.industrial < 6) return "industrial";
if (tech.electronics < 3) return "electronics";
return "military";
} else if (era <= TechEra::MODERN) {
if (tech.information < 5) return "information";
if (tech.aerospace < 4) return "aerospace";
return "robotics";
} else {
if (tech.quantum < 8) return "quantum";
if (tech.nanotechnology < 6) return "nanotechnology";
return "biotechnology";
}
}
string getBestFutureTech() {
const Technology& tech = nation->getTechnology();
if (tech.quantum < tech.nanotechnology) return "quantum";
if (tech.nanotechnology < tech.robotics) return "nanotechnology";
return "robotics";
}
void executeAction(const string& action) {
if (action == "agriculture") {
nation->developAgriculture();
} else if (action == "economy") {
nation->developEconomy();
} else if (action == "military") {
// 招募军队
recruitMilitary();
} else if (action == "industry") {
nation->developIndustry();
} else if (action == "health") {
nation->developHealth();
} else {
// 研究科技
nation->researchTechnology(action);
}
}
void recruitMilitary() {
int max_level = nation->getTechnology().military;
vector<string> unit_types = {"infantry", "vehicle"};
if (max_level >= 3) unit_types.push_back("artillery");
if (max_level >= 4) unit_types.push_back("aircraft");
if (max_level >= 5) unit_types.push_back("navy");
if (max_level >= 6) unit_types.push_back("special");
uniform_int_distribution<int> type_dist(0, unit_types.size() - 1);
uniform_int_distribution<int> level_dist(1, min(max_level, 5));
uniform_int_distribution<int> count_dist(50, 200);
string type = unit_types[type_dist(global_rng)];
int level = level_dist(global_rng);
int count = count_dist(global_rng);
// 根据经济状况调整招募数量
double gold = nation->getResources().gold;
if (gold < 10000) count = count_dist(global_rng) / 2;
nation->recruitMilitary(type, level, count);
}
bool shouldDeclareWar() {
if (aggression_level < 5) return false;
// 检查军事优势
double my_power = nation->getMilitary().totalPower();
// 这里可以添加检查潜在目标军事力量的逻辑
// 简化:随机决定
uniform_int_distribution<int> war_chance(0, 100);
return war_chance(global_rng) < (aggression_level * 3);
}
void declareWar() {
// 这里可以添加选择战争目标的逻辑
pr("AI考虑发动战争\n", 12);
}
};
// 游戏主类
class Game {
private:
vector<shared_ptr<Nation>> nations;
int currentPlayer;
int totalTurns;
bool gameRunning;
string saveFileName;
public:
Game() : currentPlayer(0), totalTurns(0), gameRunning(true), saveFileName("game_save.dat") {
initializeNations();
setupInitialDiplomacy();
}
void run() {
displayWelcome();
while (gameRunning) {
auto& currentNation = nations[currentPlayer];
if (!currentNation->isDefeated()) {
displayTurnHeader(currentNation);
currentNation->displayStatus();
if (currentPlayer == 0) { // 玩家回合
playerTurn(currentNation);
} else { // AI回合
aiTurn(currentNation);
}
// 回合结束处理
currentNation->produceResources();
// 检查游戏结束
if (checkGameEnd()) {
break;
}
}
// 切换到下一个玩家
currentPlayer = (currentPlayer + 1) % nations.size();
if (currentPlayer == 0) {
totalTurns++;
// 每回合全球广播
GlobalBroadcast::broadcastWorldStatus(nations, totalTurns);
}
}
displayGameResult();
}
// 存档功能
void saveGame() {
ofstream file(saveFileName, ios::binary);
if (!file) {
pr("无法创建存档文件!\n", 12);
return;
}
// 保存游戏基础信息
file.write(reinterpret_cast<const char*>(&totalTurns), sizeof(totalTurns));
file.write(reinterpret_cast<const char*>(¤tPlayer), sizeof(currentPlayer));
// 保存国家数量
int nationCount = nations.size();
file.write(reinterpret_cast<const char*>(&nationCount), sizeof(nationCount));
// 保存每个国家的数据
for (const auto& nation : nations) {
saveNation(file, nation);
}
file.close();
pr("游戏已成功存档!\n", 10);
}
// 读档功能
bool loadGame() {
ifstream file(saveFileName, ios::binary);
if (!file) {
pr("找不到存档文件!\n", 12);
return false;
}
// 读取游戏基础信息
file.read(reinterpret_cast<char*>(&totalTurns), sizeof(totalTurns));
file.read(reinterpret_cast<char*>(¤tPlayer), sizeof(currentPlayer));
// 读取国家数量
int nationCount;
file.read(reinterpret_cast<char*>(&nationCount), sizeof(nationCount));
// 清空当前国家列表
nations.clear();
// 读取每个国家的数据
for (int i = 0; i < nationCount; i++) {
auto nation = loadNation(file);
if (nation) {
nations.push_back(nation);
}
}
file.close();
pr("游戏已成功读档!\n", 10);
return true;
}
private:
void displayWelcome() {
pr("╔══════════════════════════════════════════════════════════════════════════════════╗\n", 14);
pr("║ 国家治理与战争模拟器 - 终极版 ║\n", 14);
pr("║ Nation Governance & War Simulator - Ultimate ║\n", 14);
pr("╚══════════════════════════════════════════════════════════════════════════════════╝\n", 14);
pr("欢迎来到终极国家模拟游戏!管理你的国家,从石器时代发展到太空时代,征服世界!\n", 11);
pr("游戏特性:\n", 13);
pr("? 12个科技时代,从石器时代到太空时代\n", 10);
pr("? 14种科技分支,超过200个科技等级\n", 10);
pr("? 6种军事单位类型,每个类型10个等级\n", 10);
pr("? 15种建筑类型,无限升级\n", 10);
pr("? 11种资源类型,包括反物质和暗物质\n", 10);
pr("? 智能AI对手,每个都有独特策略\n", 10);
pr("? 每回合全球局势报告\n", 10);
pr("? 完整的存档和读档系统\n", 10);
pr("? 无限制回合数,无限可能性\n", 10);
pr("\n按回车键开始游戏...", 7);
cin.get();
}
void displayTurnHeader(shared_ptr<Nation> nation) {
pr(("\n════════════════════════════ 第 " + to_string(totalTurns + 1) + " 回合 ════════════════════════════\n").c_str(), 11);
if (currentPlayer == 0) {
pr(("当前控制: " + nation->getName() + " - " + nation->getLeaderName() + "\n").c_str(), 11);
} else {
pr(("AI回合: " + nation->getName() + " - " + nation->getLeaderName() + "\n").c_str(), 13);
}
pr(("科技时代: " + nation->getEraName(nation->getTechEra()) + "\n").c_str(), 14);
}
void initializeNations() {
// 创建12个具有不同特色的国家
nations.push_back(make_shared<Nation>("华夏帝国", "龙皇", Terrain::PLAINS));
nations.push_back(make_shared<Nation>("北欧王国", "雷神", Terrain::MOUNTAINS));
nations.push_back(make_shared<Nation>("森林联邦", "精灵王", Terrain::FOREST));
nations.push_back(make_shared<Nation>("沙漠王朝", "太阳王", Terrain::DESERT));
nations.push_back(make_shared<Nation>("海洋共和国", "海皇", Terrain::COASTAL));
nations.push_back(make_shared<Nation>("冰原部落", "冰雪女王", Terrain::ARCTIC));
nations.push_back(make_shared<Nation>("未来都市", "AI执政官", Terrain::URBAN));
nations.push_back(make_shared<Nation>("火山帝国", "熔岩领主", Terrain::VOLCANIC));
nations.push_back(make_shared<Nation>("东方王朝", "天子", Terrain::PLAINS));
nations.push_back(make_shared<Nation>("西部联邦", "大酋长", Terrain::FOREST));
nations.push_back(make_shared<Nation>("南方帝国", "法老", Terrain::DESERT));
nations.push_back(make_shared<Nation>("北方王国", "冰霜巨人", Terrain::ARCTIC));
}
void setupInitialDiplomacy() {
// 设置复杂的外交关系网
uniform_int_distribution<int> dist_diplo(0, 2);
uniform_int_distribution<int> dist_diplo2(0, 3);
for (int i = 0; i < nations.size(); i++) {
for (int j = i + 1; j < nations.size(); j++) {
if (dist_diplo(global_rng) == 0) {
nations[i]->setRelation(nations[j]->getName(), Relation::ENEMY);
nations[j]->setRelation(nations[i]->getName(), Relation::ENEMY);
} else if (dist_diplo2(global_rng) == 0) {
nations[i]->setRelation(nations[j]->getName(), Relation::ALLY);
nations[j]->setRelation(nations[i]->getName(), Relation::ALLY);
} else {
nations[i]->setRelation(nations[j]->getName(), Relation::NEUTRAL);
nations[j]->setRelation(nations[i]->getName(), Relation::NEUTRAL);
}
}
}
}
void playerTurn(shared_ptr<Nation> nation) {
int choice;
do {
cout << "\n═══════════════ 行动菜单 ═══════════════" << endl;
cout << " 1. 发展经济 2. 发展农业 3. 发展医疗" << endl;
cout << " 4. 发展工业 5. 发展教育 6. 升级建筑" << endl;
cout << " 7. 研究科技 8. 招募军队 9. 升级军队" << endl;
cout << "10. 发动战争 11. 外交关系 12. 军事详情" << endl;
cout << "13. 保存游戏 14. 加载游戏 15. 结束回合" << endl;
cout << "═══════════════════════════════════════════" << endl;
cout << "选择行动: ";
cin >> choice;
switch(choice) {
case 1:
nation->developEconomy();
break;
case 2:
nation->developAgriculture();
break;
case 3:
nation->developHealth();
break;
case 4:
nation->developIndustry();
break;
case 5:
nation->developEducation();
break;
case 6:
buildingMenu(nation);
break;
case 7:
researchMenu(nation);
break;
case 8:
recruitMenu(nation);
break;
case 9:
upgradeMilitaryMenu(nation);
break;
case 10:
declareWar(nation);
break;
case 11:
nation->displayDiplomacy();
break;
case 12:
nation->getMilitary().displayForces();
break;
case 13:
saveGame();
break;
case 14:
if (loadGame()) {
pr("读档成功!游戏状态已恢复。\n", 10);
return; // 结束当前回合,因为游戏状态已改变
}
break;
case 15:
pr("结束回合\n", 13);
break;
default:
pr("无效选择\n", 12);
}
} while (choice != 15);
}
void buildingMenu(shared_ptr<Nation> nation) {
cout << "\n════════════ 建筑升级 ════════════" << endl;
cout << " 1. 农场 (等级: " << nation->getBuildings().farms << ")" << endl;
cout << " 2. 矿场 (等级: " << nation->getBuildings().mines << ")" << endl;
cout << " 3. 伐木场 (等级: " << nation->getBuildings().lumber_mills << ")" << endl;
cout << " 4. 兵营 (等级: " << nation->getBuildings().barracks << ")" << endl;
cout << " 5. 大学 (等级: " << nation->getBuildings().universities << ")" << endl;
cout << " 6. 医院 (等级: " << nation->getBuildings().hospitals << ")" << endl;
cout << " 7. 工厂 (等级: " << nation->getBuildings().factories << ")" << endl;
cout << " 8. 发电厂 (等级: " << nation->getBuildings().power_plants << ")" << endl;
cout << " 9. 研究所 (等级: " << nation->getBuildings().research_labs << ")" << endl;
cout << "10. 军事学院 (等级: " << nation->getBuildings().military_academy << ")" << endl;
cout << "11. 机场 (等级: " << nation->getBuildings().airports << ")" << endl;
cout << "12. 海军基地 (等级: " << nation->getBuildings().naval_bases << ")" << endl;
cout << "13. 太空港 (等级: " << nation->getBuildings().space_ports << ")" << endl;
cout << "14. AI核心 (等级: " << nation->getBuildings().ai_cores << ")" << endl;
cout << "15. 量子计算机 (等级: " << nation->getBuildings().quantum_computers << ")" << endl;
cout << "16. 返回" << endl;
cout << "选择要升级的建筑: ";
int choice;
cin >> choice;
switch(choice) {
case 1: nation->upgradeBuilding("farm"); break;
case 2: nation->upgradeBuilding("mine"); break;
case 3: nation->upgradeBuilding("lumber"); break;
case 4: nation->upgradeBuilding("barracks"); break;
case 5: nation->upgradeBuilding("university"); break;
case 6: nation->upgradeBuilding("hospital"); break;
case 7: nation->upgradeBuilding("factory"); break;
case 8: nation->upgradeBuilding("power_plant"); break;
case 9: nation->upgradeBuilding("research_lab"); break;
case 10: nation->upgradeBuilding("military_academy"); break;
case 11: nation->upgradeBuilding("airport"); break;
case 12: nation->upgradeBuilding("naval_base"); break;
case 13: nation->upgradeBuilding("space_port"); break;
case 14: nation->upgradeBuilding("ai_core"); break;
case 15: nation->upgradeBuilding("quantum_computer"); break;
case 16: break;
default: pr("无效选择\n", 12);
}
}
void researchMenu(shared_ptr<Nation> nation) {
TechEra current_era = nation->getTechEra();
cout << "\n════════════ 科技研究 ════════════" << endl;
cout << "当前科技时代: " << nation->getEraName(current_era) << endl;
cout << "基础科技:" << endl;
cout << " 1. 农业科技 (等级: " << nation->getTechnology().agriculture << "/20)" << endl;
cout << " 2. 军事科技 (等级: " << nation->getTechnology().military << "/20)" << endl;
cout << " 3. 经济科技 (等级: " << nation->getTechnology().economic << "/20)" << endl;
cout << " 4. 医疗科技 (等级: " << nation->getTechnology().medical << "/20)" << endl;
cout << " 5. 工业科技 (等级: " << nation->getTechnology().industrial << "/20)" << endl;
cout << " 6. 建筑科技 (等级: " << nation->getTechnology().construction << "/20)" << endl;
if (current_era >= TechEra::INDUSTRIAL) {
cout << "工业科技:" << endl;
cout << " 7. 电子科技 (等级: " << nation->getTechnology().electronics << "/20)" << endl;
cout << " 8. 航空航天 (等级: " << nation->getTechnology().aerospace << "/20)" << endl;
cout << " 9. 核能科技 (等级: " << nation->getTechnology().nuclear << "/20)" << endl;
}
if (current_era >= TechEra::MODERN) {
cout << "现代科技:" << endl;
cout << "10. 生物技术 (等级: " << nation->getTechnology().biotechnology << "/20)" << endl;
cout << "11. 信息技术 (等级: " << nation->getTechnology().information << "/20)" << endl;
cout << "12. 机器人技术 (等级: " << nation->getTechnology().robotics << "/20)" << endl;
}
if (current_era >= TechEra::DIGITAL) {
cout << "未来科技:" << endl;
cout << "13. 纳米技术 (等级: " << nation->getTechnology().nanotechnology << "/20)" << endl;
cout << "14. 量子科技 (等级: " << nation->getTechnology().quantum << "/20)" << endl;
}
cout << "15. 返回" << endl;
cout << "选择要研究的科技: ";
int choice;
cin >> choice;
switch(choice) {
case 1: nation->researchTechnology("agriculture"); break;
case 2: nation->researchTechnology("military"); break;
case 3: nation->researchTechnology("economic"); break;
case 4: nation->researchTechnology("medical"); break;
case 5: nation->researchTechnology("industrial"); break;
case 6: nation->researchTechnology("construction"); break;
case 7:
if (current_era >= TechEra::INDUSTRIAL) nation->researchTechnology("electronics");
else pr("尚未达到工业时代,无法研究该科技\n", 12);
break;
case 8:
if (current_era >= TechEra::INDUSTRIAL) nation->researchTechnology("aerospace");
else pr("尚未达到工业时代,无法研究该科技\n", 12);
break;
case 9:
if (current_era >= TechEra::INDUSTRIAL) nation->researchTechnology("nuclear");
else pr("尚未达到工业时代,无法研究该科技\n", 12);
break;
case 10:
if (current_era >= TechEra::MODERN) nation->researchTechnology("biotechnology");
else pr("尚未达到现代,无法研究该科技\n", 12);
break;
case 11:
if (current_era >= TechEra::MODERN) nation->researchTechnology("information");
else pr("尚未达到现代,无法研究该科技\n", 12);
break;
case 12:
if (current_era >= TechEra::MODERN) nation->researchTechnology("robotics");
else pr("尚未达到现代,无法研究该科技\n", 12);
break;
case 13:
if (current_era >= TechEra::DIGITAL) nation->researchTechnology("nanotechnology");
else pr("尚未达到数字时代,无法研究该科技\n", 12);
break;
case 14:
if (current_era >= TechEra::DIGITAL) nation->researchTechnology("quantum");
else pr("尚未达到数字时代,无法研究该科技\n", 12);
break;
case 15: break;
default: pr("无效选择\n", 12);
}
}
void recruitMenu(shared_ptr<Nation> nation) {
int max_tech_level = nation->getTechnology().military;
cout << "\n════════════ 军队招募 ════════════" << endl;
cout << "当前军事科技等级: " << max_tech_level << " (最高可招募等级 " << max_tech_level << " 单位)" << endl;
auto available_units = nation->getMilitary().getAvailableUnits(max_tech_level);
map<string, vector<MilitaryUnit*>> units_by_type;
for (auto unit : available_units) {
units_by_type[unit->type].push_back(unit);
}
int index = 1;
map<int, pair<string, int>> unit_map;
for (const auto& type_pair : units_by_type) {
const string& type = type_pair.first;
const vector<MilitaryUnit*>& type_units = type_pair.second;
string type_name;
if (type == "infantry") type_name = "步兵";
else if (type == "vehicle") type_name = "车辆";
else if (type == "aircraft") type_name = "空军";
else if (type == "navy") type_name = "海军";
else if (type == "special") type_name = "特殊部队";
cout << "\n" << type_name << ":\n";
for (const auto unit : type_units) {
cout << " " << index << ". " << unit->name << " (等级 " << unit->level << ")";
cout << " | 攻击: " << unit->attack_power << " | 防御: " << unit->defense_power;
cout << " | 移动: " << unit->mobility << " | 成本: " << (15 * unit->level) << "黄金\n";
unit_map[index] = make_pair(unit->type, unit->level);
index++;
}
}
cout << index << ". 返回" << endl;
cout << "选择兵种: ";
int choice, count;
cin >> choice;
if (choice > 0 && choice < index) {
auto selected = unit_map[choice];
cout << "招募数量: ";
cin >> count;
nation->recruitMilitary(selected.first, selected.second, count);
}
}
void upgradeMilitaryMenu(shared_ptr<Nation> nation) {
cout << "\n════════════ 军队升级 ════════════" << endl;
nation->getMilitary().displayForces();
// 简化的升级界面
pr("军队升级功能开发中...\n", 13);
}
void declareWar(shared_ptr<Nation> attacker) {
cout << "\n════════════ 宣战目标 ════════════" << endl;
int index = 1;
vector<int> valid_targets;
for (int i = 0; i < nations.size(); i++) {
if (nations[i]->getName() != attacker->getName() && !nations[i]->isDefeated()) {
cout << index << ". " << nations[i]->getName();
Relation rel = attacker->getRelation(nations[i]->getName());
switch(rel) {
case Relation::ALLY: cout << " (同盟)"; break;
case Relation::NEUTRAL: cout << " (中立)"; break;
case Relation::ENEMY: cout << " (敌国)"; break;
case Relation::VASSAL: cout << " (附庸)"; break;
case Relation::TRADE_PARTNER: cout << " (贸易伙伴)"; break;
}
cout << " - 战力: " << static_cast<int>(nations[i]->getMilitary().totalPower());
cout << " - 时代: " << nations[i]->getEraName(nations[i]->getTechEra()) << endl;
valid_targets.push_back(i);
index++;
}
}
cout << index << ". 返回" << endl;
cout << "选择目标: ";
int choice;
cin >> choice;
if (choice > 0 && choice <= valid_targets.size()) {
int target_index = valid_targets[choice - 1];
BattleSystem::conductBattle(*attacker, *nations[target_index]);
// 更新外交关系
attacker->setRelation(nations[target_index]->getName(), Relation::ENEMY);
nations[target_index]->setRelation(attacker->getName(), Relation::ENEMY);
}
}
void aiTurn(shared_ptr<Nation> nation) {
pr("AI正在思考...\n", 13);
this_thread::sleep_for(chrono::seconds(1)); // 模拟AI思考
AIStrategy strategy(nation);
strategy.executeTurn();
pr("AI回合结束\n", 13);
}
bool checkGameEnd() {
// 检查是否有玩家失败
int alive_nations = 0;
for (const auto& nation : nations) {
if (!nation->isDefeated()) {
alive_nations++;
}
}
if (alive_nations <= 1) {
gameRunning = false;
return true;
}
// 玩家失败
if (nations[0]->isDefeated()) {
gameRunning = false;
return true;
}
return false;
}
void displayGameResult() {
pr("\n════════════ 游戏结束 ════════════\n", 14);
// 找到胜利者
shared_ptr<Nation> winner = nullptr;
double max_score = 0;
for (const auto& nation : nations) {
if (!nation->isDefeated()) {
double score = nation->getEconomy() + nation->getMilitary().totalPower() / 10000 +
nation->getGlobalInfluence() + static_cast<int>(nation->getTechEra()) * 1000;
if (score > max_score) {
max_score = score;
winner = nation;
}
}
}
if (winner) {
pr(("胜利者: " + winner->getName() + " - " + winner->getLeaderName() + "\n").c_str(), 10);
pr(("最终时代: " + winner->getEraName(winner->getTechEra()) + "\n").c_str(), 13);
pr(("游戏回合: " + to_string(totalTurns) + "\n").c_str(), 11);
pr("恭喜获得最终胜利!\n", 14);
} else {
pr("所有国家都已灭亡,没有胜利者\n", 12);
}
// 显示最终排名
displayFinalRankings();
}
void displayFinalRankings() {
pr("\n最终排名:\n", 11);
vector<tuple<double, string, TechEra, int, double>> rankings;
for (const auto& nation : nations) {
double score = nation->getEconomy() + nation->getMilitary().totalPower() / 10000 +
nation->getGlobalInfluence() + static_cast<int>(nation->getTechEra()) * 1000;
rankings.emplace_back(score, nation->getName(), nation->getTechEra(),
nation->getPopulation(), nation->getMilitary().totalPower());
}
sort(rankings.rbegin(), rankings.rend());
for (size_t i = 0; i < rankings.size(); ++i) {
double score;
string name;
TechEra era;
int pop;
double military;
tie(score, name, era, pop, military) = rankings[i];
cout << i + 1 << ". " << name
<< " - 分数: " << static_cast<int>(score)
<< " - 时代: " << getEraName(era)
<< " - 人口: " << (pop > 1000000 ? to_string(pop/1000000) + "M" : to_string(pop/1000) + "K")
<< " - 军力: " << (military > 1000000 ? to_string(static_cast<int>(military)/1000000) + "M" :
military > 1000 ? to_string(static_cast<int>(military)/1000) + "K" : to_string(static_cast<int>(military)))
<< endl;
}
}
string getEraName(TechEra era) {
switch(era) {
case TechEra::STONE_AGE: return "石器时代";
case TechEra::BRONZE_AGE: return "青铜时代";
case TechEra::IRON_AGE: return "铁器时代";
case TechEra::CLASSICAL: return "古典时代";
case TechEra::MEDIEVAL: return "中世纪";
case TechEra::RENAISSANCE: return "文艺复兴";
case TechEra::INDUSTRIAL: return "工业时代";
case TechEra::MODERN: return "现代";
case TechEra::INFORMATION: return "信息时代";
case TechEra::DIGITAL: return "数字时代";
case TechEra::BIOTECH: return "生物科技";
case TechEra::SPACE_AGE: return "太空时代";
default: return "未知";
}
}
// 保存单个国家数据
void saveNation(ofstream& file, const shared_ptr<Nation>& nation) {
// 保存基础信息
string name = nation->getName();
string leader = nation->getLeaderName();
int nameLen = name.length();
int leaderLen = leader.length();
file.write(reinterpret_cast<const char*>(&nameLen), sizeof(nameLen));
file.write(name.c_str(), nameLen);
file.write(reinterpret_cast<const char*>(&leaderLen), sizeof(leaderLen));
file.write(leader.c_str(), leaderLen);
// 保存地形
Terrain terrain = nation->getTerrain();
file.write(reinterpret_cast<const char*>(&terrain), sizeof(terrain));
// 保存资源
Resources res = nation->getResources();
file.write(reinterpret_cast<const char*>(&res), sizeof(res));
// 保存科技
Technology tech = nation->getTechnology();
file.write(reinterpret_cast<const char*>(&tech), sizeof(tech));
// 保存建筑
Buildings buildings = nation->getBuildings();
file.write(reinterpret_cast<const char*>(&buildings), sizeof(buildings));
// 保存国家属性
double economy = nation->getEconomy();
double agriculture = nation->getAgriculture();
double health = nation->getHealth();
double stability = nation->getStability();
double happiness = nation->getHappiness();
double education = nation->getEducation();
int population = nation->getPopulation();
int territory_size = nation->getTerritorySize();
double inflation = nation->getInflation();
int turn_count = nation->getTurnCount();
double global_influence = nation->getGlobalInfluence();
file.write(reinterpret_cast<const char*>(&economy), sizeof(economy));
file.write(reinterpret_cast<const char*>(&agriculture), sizeof(agriculture));
file.write(reinterpret_cast<const char*>(&health), sizeof(health));
file.write(reinterpret_cast<const char*>(&stability), sizeof(stability));
file.write(reinterpret_cast<const char*>(&happiness), sizeof(happiness));
file.write(reinterpret_cast<const char*>(&education), sizeof(education));
file.write(reinterpret_cast<const char*>(&population), sizeof(population));
file.write(reinterpret_cast<const char*>(&territory_size), sizeof(territory_size));
file.write(reinterpret_cast<const char*>(&inflation), sizeof(inflation));
file.write(reinterpret_cast<const char*>(&turn_count), sizeof(turn_count));
file.write(reinterpret_cast<const char*>(&global_influence), sizeof(global_influence));
// 保存军事单位
auto military_units = nation->getMilitaryUnits();
int unit_count = military_units.size();
file.write(reinterpret_cast<const char*>(&unit_count), sizeof(unit_count));
for (const auto& unit : military_units) {
int type_len = unit.type.length();
file.write(reinterpret_cast<const char*>(&type_len), sizeof(type_len));
file.write(unit.type.c_str(), type_len);
file.write(reinterpret_cast<const char*>(&unit.level), sizeof(unit.level));
file.write(reinterpret_cast<const char*>(&unit.count), sizeof(unit.count));
}
// 保存外交关系
auto relations = nation->getAllRelations();
int relationCount = relations.size();
file.write(reinterpret_cast<const char*>(&relationCount), sizeof(relationCount));
for (const auto& rel : relations) {
int nationNameLen = rel.first.length();
file.write(reinterpret_cast<const char*>(&nationNameLen), sizeof(nationNameLen));
file.write(rel.first.c_str(), nationNameLen);
file.write(reinterpret_cast<const char*>(&rel.second), sizeof(rel.second));
}
}
// 加载单个国家数据
shared_ptr<Nation> loadNation(ifstream& file) {
// 读取基础信息
int nameLen, leaderLen;
file.read(reinterpret_cast<char*>(&nameLen), sizeof(nameLen));
string name(nameLen, ' ');
file.read(&name[0], nameLen);
file.read(reinterpret_cast<char*>(&leaderLen), sizeof(leaderLen));
string leader(leaderLen, ' ');
file.read(&leader[0], leaderLen);
// 读取地形
Terrain terrain;
file.read(reinterpret_cast<char*>(&terrain), sizeof(terrain));
auto nation = make_shared<Nation>(name, leader, terrain);
// 读取资源
Resources res;
file.read(reinterpret_cast<char*>(&res), sizeof(res));
nation->getResources() = res;
// 读取科技
Technology tech;
file.read(reinterpret_cast<char*>(&tech), sizeof(tech));
nation->getTechnology() = tech;
// 读取建筑
Buildings buildings;
file.read(reinterpret_cast<char*>(&buildings), sizeof(buildings));
nation->getBuildings() = buildings;
// 读取国家属性
double economy, agriculture, health, stability, happiness, education, inflation, global_influence;
int population, territory_size, turn_count;
file.read(reinterpret_cast<char*>(&economy), sizeof(economy));
file.read(reinterpret_cast<char*>(&agriculture), sizeof(agriculture));
file.read(reinterpret_cast<char*>(&health), sizeof(health));
file.read(reinterpret_cast<char*>(&stability), sizeof(stability));
file.read(reinterpret_cast<char*>(&happiness), sizeof(happiness));
file.read(reinterpret_cast<char*>(&education), sizeof(education));
file.read(reinterpret_cast<char*>(&population), sizeof(population));
file.read(reinterpret_cast<char*>(&territory_size), sizeof(territory_size));
file.read(reinterpret_cast<char*>(&inflation), sizeof(inflation));
file.read(reinterpret_cast<char*>(&turn_count), sizeof(turn_count));
file.read(reinterpret_cast<char*>(&global_influence), sizeof(global_influence));
// 设置国家属性
nation->setAgriculture(agriculture);
nation->setHealth(health);
nation->setStability(stability);
nation->setHappiness(happiness);
nation->setEducation(education);
nation->setPopulation(population);
nation->setTerritorySize(territory_size);
nation->setInflation(inflation);
nation->setTurnCount(turn_count);
nation->setGlobalInfluence(global_influence);
// 读取军事单位
int unit_count;
file.read(reinterpret_cast<char*>(&unit_count), sizeof(unit_count));
for (int i = 0; i < unit_count; i++) {
int type_len;
file.read(reinterpret_cast<char*>(&type_len), sizeof(type_len));
string type(type_len, ' ');
file.read(&type[0], type_len);
int level, count;
file.read(reinterpret_cast<char*>(&level), sizeof(level));
file.read(reinterpret_cast<char*>(&count), sizeof(count));
nation->setMilitaryUnitCount(type, level, count);
}
// 读取外交关系
int relationCount;
file.read(reinterpret_cast<char*>(&relationCount), sizeof(relationCount));
for (int i = 0; i < relationCount; i++) {
int nationNameLen;
file.read(reinterpret_cast<char*>(&nationNameLen), sizeof(nationNameLen));
string otherNation(nationNameLen, ' ');
file.read(&otherNation[0], nationNameLen);
Relation rel;
file.read(reinterpret_cast<char*>(&rel), sizeof(rel));
nation->setRelation(otherNation, rel);
}
return nation;
}
};
int main()
{
Game game;
game.run();
return 0;
}
#include <bits/stdc++.h>
#include <random>
#include <memory>
#include <windows.h>
#include <iomanip>
#include <unordered_map>
#include <fstream>
#include <sstream>
#include <thread>
#include <chrono>
using namespace std;
// 颜色控制函数
void pr(const char* s, int color) {
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | color);
printf(s);
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | 7);
}
// 地形类型
enum class Terrain {
PLAINS, // 平原
MOUNTAINS, // 山脉
FOREST, // 森林
DESERT, // 沙漠
COASTAL, // 沿海
ARCTIC, // 极地
URBAN, // 城市
VOLCANIC // 火山
};
// 国家关系
enum class Relation {
ALLY, // 同盟
NEUTRAL, // 中立
ENEMY, // 敌国
VASSAL, // 附庸
TRADE_PARTNER // 贸易伙伴
};
// 科技时代
enum class TechEra {
STONE_AGE, // 石器时代
BRONZE_AGE, // 青铜时代
IRON_AGE, // 铁器时代
CLASSICAL, // 古典时代
MEDIEVAL, // 中世纪
RENAISSANCE, // 文艺复兴
INDUSTRIAL, // 工业时代
MODERN, // 现代
INFORMATION, // 信息时代
DIGITAL, // 数字时代
BIOTECH, // 生物科技时代
SPACE_AGE // 太空时代
};
// 详细的科技等级结构
struct Technology {
// 基础科技 (1-20)
int agriculture; // 农业科技
int military; // 军事科技
int economic; // 经济科技
int medical; // 医疗科技
int industrial; // 工业科技
int construction; // 建筑科技
// 高级科技分支 (0-20)
int electronics; // 电子科技
int aerospace; // 航空航天
int nuclear; // 核能科技
int biotechnology; // 生物技术
int information; // 信息技术
int robotics; // 机器人技术
int nanotechnology; // 纳米技术
int quantum; // 量子科技
// 特殊科技标志
bool has_advanced_medicine;
bool has_steel_production;
bool has_navigation;
bool has_electricity;
bool has_computers;
bool has_internet;
bool has_ai;
bool has_fusion_power;
bool has_teleportation;
bool has_time_travel;
Technology() :
agriculture(1), military(1), economic(1), medical(1), industrial(1), construction(1),
electronics(0), aerospace(0), nuclear(0), biotechnology(0), information(0),
robotics(0), nanotechnology(0), quantum(0),
has_advanced_medicine(false), has_steel_production(false),
has_navigation(false), has_electricity(false), has_computers(false),
has_internet(false), has_ai(false), has_fusion_power(false),
has_teleportation(false), has_time_travel(false) {}
// 获取当前科技时代
TechEra getCurrentEra() const {
double tech_score = (agriculture + military + economic + medical + industrial + construction +
electronics + aerospace + nuclear + biotechnology + information +
robotics + nanotechnology + quantum) / 14.0;
if (tech_score >= 18) return TechEra::SPACE_AGE;
if (tech_score >= 16) return TechEra::BIOTECH;
if (tech_score >= 14) return TechEra::DIGITAL;
if (tech_score >= 12) return TechEra::INFORMATION;
if (tech_score >= 10) return TechEra::MODERN;
if (tech_score >= 8) return TechEra::INDUSTRIAL;
if (tech_score >= 6) return TechEra::RENAISSANCE;
if (tech_score >= 4) return TechEra::MEDIEVAL;
if (tech_score >= 3) return TechEra::CLASSICAL;
if (tech_score >= 2) return TechEra::IRON_AGE;
if (tech_score >= 1.5) return TechEra::BRONZE_AGE;
return TechEra::STONE_AGE;
}
// 检查科技前提
bool canResearch(const string& tech_name) const {
if (tech_name == "electronics" && economic < 3) return false;
if (tech_name == "aerospace" && electronics < 2) return false;
if (tech_name == "nuclear" && industrial < 5) return false;
if (tech_name == "biotechnology" && medical < 4) return false;
if (tech_name == "information" && electronics < 4) return false;
if (tech_name == "robotics" && information < 3) return false;
if (tech_name == "nanotechnology" && robotics < 4) return false;
if (tech_name == "quantum" && nanotechnology < 5) return false;
return true;
}
};
// 建筑等级
struct Buildings {
int farms; // 农场
int mines; // 矿场
int lumber_mills; // 伐木场
int barracks; // 兵营
int universities; // 大学
int hospitals; // 医院
int factories; // 工厂
int power_plants; // 发电厂
int research_labs; // 研究所
int military_academy; // 军事学院
int airports; // 机场
int naval_bases; // 海军基地
int space_ports; // 太空港
int ai_cores; // AI核心
int quantum_computers; // 量子计算机
Buildings() :
farms(1), mines(1), lumber_mills(1), barracks(1),
universities(0), hospitals(0), factories(0),
power_plants(0), research_labs(0), military_academy(0),
airports(0), naval_bases(0), space_ports(0), ai_cores(0), quantum_computers(0) {}
};
// 军队单位结构
struct MilitaryUnit {
string name;
string type; // "infantry", "vehicle", "aircraft", "navy", "artillery", "special", "space"
int level; // 1-10级
int count;
double attack_power;
double defense_power;
double mobility;
double range;
double maintenance_cost;
vector<string> special_abilities;
MilitaryUnit(string n, string t, int lvl, double attack, double defense, double mob, double rng, double cost)
: name(n), type(t), level(lvl), count(0), attack_power(attack), defense_power(defense),
mobility(mob), range(rng), maintenance_cost(cost) {}
void addAbility(const string& ability) {
special_abilities.push_back(ability);
}
};
// 军事力量结构
class MilitaryForces {
private:
vector<MilitaryUnit> units;
public:
MilitaryForces() {
initializeUnits();
}
void initializeUnits() {
// 步兵单位 (1-10级)
units.emplace_back("部落战士", "infantry", 1, 15, 8, 2, 1, 1);
units.emplace_back("正规军", "infantry", 2, 30, 15, 3, 1, 2);
units.emplace_back("特种部队", "infantry", 3, 60, 30, 4, 2, 4);
units.emplace_back("未来战士", "infantry", 4, 120, 60, 6, 3, 8);
units.emplace_back("基因战士", "infantry", 5, 240, 120, 8, 4, 16);
units.emplace_back("纳米战士", "infantry", 6, 480, 240, 10, 6, 32);
units.emplace_back("能量战士", "infantry", 7, 960, 480, 12, 8, 64);
units.emplace_back("量子战士", "infantry", 8, 1920, 960, 15, 10, 128);
units.emplace_back("时空战士", "infantry", 9, 3840, 1920, 18, 12, 256);
units.emplace_back("神级战士", "infantry", 10, 7680, 3840, 20, 15, 512);
// 车辆单位
units.emplace_back("战车", "vehicle", 1, 40, 25, 4, 2, 6);
units.emplace_back("坦克", "vehicle", 2, 80, 50, 5, 3, 12);
units.emplace_back("主战坦克", "vehicle", 3, 160, 100, 6, 4, 24);
units.emplace_back("未来坦克", "vehicle", 4, 320, 200, 8, 6, 48);
units.emplace_back("机甲部队", "vehicle", 5, 640, 400, 10, 8, 96);
units.emplace_back("纳米机甲", "vehicle", 6, 1280, 800, 12, 10, 192);
units.emplace_back("量子坦克", "vehicle", 7, 2560, 1600, 14, 12, 384);
units.emplace_back("时空战车", "vehicle", 8, 5120, 3200, 16, 14, 768);
units.emplace_back("反物质坦克", "vehicle", 9, 10240, 6400, 18, 16, 1536);
units.emplace_back("神级战车", "vehicle", 10, 20480, 12800, 20, 18, 3072);
// 空军单位
units.emplace_back("战斗机", "aircraft", 1, 60, 25, 10, 8, 18);
units.emplace_back("喷气式战机", "aircraft", 2, 120, 50, 12, 10, 36);
units.emplace_back("隐形战机", "aircraft", 3, 240, 100, 15, 12, 72);
units.emplace_back("太空战机", "aircraft", 4, 480, 200, 20, 15, 144);
units.emplace_back("无人机群", "aircraft", 5, 960, 400, 25, 20, 288);
units.emplace_back("量子战机", "aircraft", 6, 1920, 800, 30, 25, 576);
units.emplace_back("时空战机", "aircraft", 7, 3840, 1600, 35, 30, 1152);
units.emplace_back("反物质战机", "aircraft", 8, 7680, 3200, 40, 35, 2304);
units.emplace_back("星际战机", "aircraft", 9, 15360, 6400, 45, 40, 4608);
units.emplace_back("神级战机", "aircraft", 10, 30720, 12800, 50, 45, 9216);
// 海军单位
units.emplace_back("护卫舰", "navy", 1, 50, 35, 6, 5, 14);
units.emplace_back("驱逐舰", "navy", 2, 100, 70, 7, 6, 28);
units.emplace_back("航空母舰", "navy", 3, 200, 140, 8, 10, 56);
units.emplace_back("核动力航母", "navy", 4, 400, 280, 9, 12, 112);
units.emplace_back("太空战舰", "navy", 5, 800, 560, 12, 15, 224);
units.emplace_back("量子战舰", "navy", 6, 1600, 1120, 14, 18, 448);
units.emplace_back("时空战舰", "navy", 7, 3200, 2240, 16, 20, 896);
units.emplace_back("反物质战舰", "navy", 8, 6400, 4480, 18, 22, 1792);
units.emplace_back("星际母舰", "navy", 9, 12800, 8960, 20, 25, 3584);
units.emplace_back("神级战舰", "navy", 10, 25600, 17920, 22, 28, 7168);
// 特殊单位
units.emplace_back("工程师", "special", 1, 10, 8, 4, 2, 3);
units.emplace_back("间谍", "special", 2, 15, 12, 6, 3, 5);
units.emplace_back("网络战部队", "special", 3, 25, 18, 5, 10, 12);
units.emplace_back("卫星武器", "special", 4, 50, 30, 0, 100, 60);
units.emplace_back("纳米机器人", "special", 5, 100, 60, 12, 8, 120);
units.emplace_back("量子特工", "special", 6, 200, 120, 15, 12, 240);
units.emplace_back("时空特工", "special", 7, 400, 240, 18, 15, 480);
units.emplace_back("AI军团", "special", 8, 800, 480, 20, 18, 960);
units.emplace_back("反物质部队", "special", 9, 1600, 960, 22, 20, 1920);
units.emplace_back("神级特工", "special", 10, 3200, 1920, 25, 22, 3840);
// 添加特殊能力
for (auto& unit : units) {
if (unit.level >= 5) unit.addAbility("高级装备");
if (unit.level >= 7) unit.addAbility("能量护盾");
if (unit.level >= 9) unit.addAbility("量子隐形");
if (unit.type == "special" && unit.level >= 3) unit.addAbility("情报收集");
if (unit.type == "aircraft" && unit.level >= 4) unit.addAbility("超音速");
if (unit.type == "navy" && unit.level >= 6) unit.addAbility("深海潜行");
}
}
MilitaryUnit* getUnit(const string& type, int level) {
for (auto& unit : units) {
if (unit.type == type && unit.level == level) {
return &unit;
}
}
return nullptr;
}
vector<MilitaryUnit*> getAvailableUnits(int max_tech_level) {
vector<MilitaryUnit*> available;
for (auto& unit : units) {
if (unit.level <= max_tech_level) {
available.push_back(&unit);
}
}
return available;
}
bool recruitUnit(const string& type, int level, int count) {
MilitaryUnit* unit = getUnit(type, level);
if (unit) {
unit->count += count;
return true;
}
return false;
}
bool upgradeUnit(const string& type, int from_level, int to_level, int count) {
MilitaryUnit* from_unit = getUnit(type, from_level);
MilitaryUnit* to_unit = getUnit(type, to_level);
if (from_unit && to_unit && from_unit->count >= count) {
from_unit->count -= count;
to_unit->count += count;
return true;
}
return false;
}
double totalPower() const {
double power = 0;
for (const auto& unit : units) {
power += unit.count * (unit.attack_power + unit.defense_power);
// 特殊能力加成
for (const auto& ability : unit.special_abilities) {
if (ability == "高级装备") power += unit.count * 10;
if (ability == "能量护盾") power += unit.count * 20;
if (ability == "量子隐形") power += unit.count * 30;
}
}
return power;
}
double totalMaintenance() const {
double cost = 0;
for (const auto& unit : units) {
cost += unit.count * unit.maintenance_cost;
}
return cost;
}
void displayForces() const {
pr("\n=== 军事力量详情 ===\n", 12);
map<string, vector<const MilitaryUnit*>> units_by_type;
for (const auto& unit : units) {
if (unit.count > 0) {
units_by_type[unit.type].push_back(&unit);
}
}
for (const auto& type_pair : units_by_type) {
const string& type = type_pair.first;
const vector<const MilitaryUnit*>& type_units = type_pair.second;
string type_name;
if (type == "infantry") type_name = "步兵";
else if (type == "vehicle") type_name = "车辆";
else if (type == "aircraft") type_name = "空军";
else if (type == "navy") type_name = "海军";
else if (type == "special") type_name = "特殊部队";
cout << "\n" << type_name << ":\n";
for (const auto unit : type_units) {
cout << " " << unit->name << " (等级 " << unit->level << "): " << unit->count
<< " 单位 | 攻击: " << unit->attack_power
<< " | 防御: " << unit->defense_power;
if (!unit->special_abilities.empty()) {
cout << " | 能力: ";
for (const auto& ability : unit->special_abilities) {
cout << ability << " ";
}
}
cout << endl;
}
}
cout << "\n总战力: " << totalPower() << endl;
cout << "总维护费用: " << totalMaintenance() << " 黄金/回合" << endl;
}
// 获取所有单位
const vector<MilitaryUnit>& getAllUnits() const {
return units;
}
// 设置单位数量(用于存档)
void setUnitCount(const string& type, int level, int count) {
for (auto& unit : units) {
if (unit.type == type && unit.level == level) {
unit.count = count;
return;
}
}
}
};
// 资源系统
struct Resources {
long long gold; // 黄金
long long food; // 食物
long long wood; // 木材
long long iron; // 铁矿石
long long oil; // 石油
long long uranium; // 铀矿
long long titanium; // 钛矿
long long antimatter; // 反物质
long long research; // 科研点数
long long energy; // 能源
long long dark_matter; // 暗物质
Resources() : gold(10000), food(20000), wood(10000), iron(5000), oil(0),
uranium(0), titanium(0), antimatter(0), research(0), energy(0), dark_matter(0) {}
};
// 全局随机数生成器
mt19937 global_rng(time(nullptr));
// 国家类
class Nation {
private:
string name;
string leader_name;
Terrain terrain;
Resources resources;
MilitaryForces military;
Technology tech;
Buildings buildings;
map<string, Relation> diplomacy;
// 国家属性
double economy; // 经济指数 0-200
double agriculture; // 农业指数 0-200
double health; // 健康指数 0-200
double stability; // 稳定度 0-200
double happiness; // 幸福度 0-200
double education; // 教育水平 0-200
int population; // 人口
int territory_size; // 领土大小
double inflation; // 通货膨胀率
// 升级相关
int turn_count;
double global_influence; // 全球影响力
public:
Nation(string n, string leader, Terrain t) :
name(n), leader_name(leader), terrain(t), economy(60),
agriculture(60), health(80), stability(75), happiness(70), education(50),
population(2000000), territory_size(2000), inflation(1.0), turn_count(0), global_influence(10) {}
// 获取国家名称
string getName() const { return name; }
string getLeaderName() const { return leader_name; }
// 获取地形
Terrain getTerrain() const { return terrain; }
// 获取军事力量
const MilitaryForces& getMilitary() const { return military; }
MilitaryForces& getMilitary() { return military; }
// 获取资源
const Resources& getResources() const { return resources; }
Resources& getResources() { return resources; }
// 获取科技
const Technology& getTechnology() const { return tech; }
Technology& getTechnology() { return tech; }
// 获取建筑
const Buildings& getBuildings() const { return buildings; }
Buildings& getBuildings() { return buildings; }
// 获取人口
int getPopulation() const { return population; }
// 获取经济指数
double getEconomy() const { return economy; }
// 获取全球影响力
double getGlobalInfluence() const { return global_influence; }
// 设置外交关系
void setRelation(const string& otherNation, Relation rel) {
diplomacy[otherNation] = rel;
}
// 获取外交关系
Relation getRelation(const string& otherNation) const {
auto it = diplomacy.find(otherNation);
if (it != diplomacy.end()) {
return it->second;
}
return Relation::NEUTRAL;
}
// 获取所有外交关系
const map<string, Relation>& getAllRelations() const {
return diplomacy;
}
// 经济发展
void developEconomy() {
long double cost = 1000 * (1 + tech.economic * 0.1);
if (resources.gold >= cost) {
resources.gold -= cost;
uniform_int_distribution<int> dist_econ(8, 22);
economy += dist_econ(global_rng) * (1 + tech.economic * 0.05);
resources.research += 15;
inflation += 0.01;
pr("经济发展成功!经济指数上升\n", 10);
} else {
pr("资金不足,无法发展经济\n", 12);
}
}
// 农业发展
void developAgriculture() {
long double cost = 2000 * (1 + tech.agriculture * 0.1);
if (resources.food >= cost * 0.5) {
resources.food -= cost * 0.5;
uniform_int_distribution<int> dist_agri(8, 19);
agriculture += dist_agri(global_rng) * (1 + tech.agriculture * 0.05);
population += 100000 * (1 + tech.agriculture * 0.1);
resources.research += 12;
pr("农业发展成功!农业指数上升,人口增长\n", 10);
} else {
pr("食物不足,无法发展农业\n", 12);
}
}
// 医疗发展
void developHealth() {
long double cost = 600 * (1 + tech.medical * 0.1);
if (resources.gold >= cost) {
resources.gold -= cost;
uniform_int_distribution<int> dist_health(6, 15);
health += dist_health(global_rng) * (1 + tech.medical * 0.05);
stability += 8;
happiness += 5;
population += 50000;
resources.research += 18;
pr("医疗发展成功!健康指数上升\n", 11);
} else {
pr("资金不足,无法发展医疗\n", 12);
}
}
// 工业发展
void developIndustry() {
double cost = 1000 * (1 + tech.industrial * 0.1);
if (resources.gold >= cost && resources.iron >= 800) {
resources.gold -= cost;
resources.iron -= 800;
economy += 12;
resources.research += 20;
pr("工业发展成功!经济指数上升\n", 13);
} else {
pr("资源不足,无法发展工业\n", 12);
}
}
// 教育发展
void developEducation() {
double cost = 500 * (1 + tech.economic * 0.1);
if (resources.gold >= cost) {
resources.gold -= cost;
uniform_int_distribution<int> dist_edu(5, 12);
education += dist_edu(global_rng) * (1 + tech.economic * 0.03);
resources.research += 25;
happiness +=5;
pr("教育发展成功!教育水平上升\n", 14);
} else {
pr("资金不足,无法发展教育\n", 12);
}
}
// 建筑升级系统
void upgradeBuilding(const string& building_type) {
double cost_multiplier = 1.0;
int* building_level = nullptr;
string building_name;
if (building_type == "farm") {
building_level = &buildings.farms;
building_name = "农场";
cost_multiplier = 100;
} else if (building_type == "mine") {
building_level = &buildings.mines;
building_name = "矿场";
cost_multiplier = 400;
} else if (building_type == "lumber") {
building_level = &buildings.lumber_mills;
building_name = "伐木场";
cost_multiplier = 350;
} else if (building_type == "barracks") {
building_level = &buildings.barracks;
building_name = "兵营";
cost_multiplier = 500;
} else if (building_type == "university") {
building_level = &buildings.universities;
building_name = "大学";
cost_multiplier = 800;
} else if (building_type == "hospital") {
building_level = &buildings.hospitals;
building_name = "医院";
cost_multiplier = 700;
} else if (building_type == "factory") {
building_level = &buildings.factories;
building_name = "工厂";
cost_multiplier = 800;
} else if (building_type == "power_plant") {
building_level = &buildings.power_plants;
building_name = "发电厂";
cost_multiplier = 800;
} else if (building_type == "research_lab") {
building_level = &buildings.research_labs;
building_name = "研究所";
cost_multiplier = 1000;
} else if (building_type == "military_academy") {
building_level = &buildings.military_academy;
building_name = "军事学院";
cost_multiplier = 1000;
} else if (building_type == "airport") {
building_level = &buildings.airports;
building_name = "机场";
cost_multiplier = 1500;
} else if (building_type == "naval_base") {
building_level = &buildings.naval_bases;
building_name = "海军基地";
cost_multiplier = 1400;
} else if (building_type == "space_port") {
building_level = &buildings.space_ports;
building_name = "太空港";
cost_multiplier = 5000;
} else if (building_type == "ai_core") {
building_level = &buildings.ai_cores;
building_name = "AI核心";
cost_multiplier = 3000;
} else if (building_type == "quantum_computer") {
building_level = &buildings.quantum_computers;
building_name = "量子计算机";
cost_multiplier = 4000;
}
if (building_level) {
double cost = cost_multiplier * (*building_level);
if (resources.gold >= cost) {
resources.gold -= cost;
(*building_level)++;
pr((building_name + "升级成功!当前等级: " + to_string(*building_level) + "\n").c_str(), 10);
} else {
pr("资金不足,无法升级建筑\n", 12);
}
}
}
// 科技研究系统
void researchTechnology(const string& tech_type) {
double research_cost = 0;
int* tech_level = nullptr;
string tech_name;
int max_level = 20;
if (tech_type == "agriculture") {
tech_level = &tech.agriculture;
tech_name = "农业科技";
research_cost = 1200;
} else if (tech_type == "military") {
tech_level = &tech.military;
tech_name = "军事科技";
research_cost = 1800;
} else if (tech_type == "economic") {
tech_level = &tech.economic;
tech_name = "经济科技";
research_cost = 1500;
} else if (tech_type == "medical") {
tech_level = &tech.medical;
tech_name = "医疗科技";
research_cost = 1400;
} else if (tech_type == "industrial") {
tech_level = &tech.industrial;
tech_name = "工业科技";
research_cost = 2000;
} else if (tech_type == "construction") {
tech_level = &tech.construction;
tech_name = "建筑科技";
research_cost = 1300;
} else if (tech_type == "electronics") {
tech_level = &tech.electronics;
tech_name = "电子科技";
research_cost = 2500;
} else if (tech_type == "aerospace") {
tech_level = &tech.aerospace;
tech_name = "航空航天";
research_cost = 3000;
} else if (tech_type == "nuclear") {
tech_level = &tech.nuclear;
tech_name = "核能科技";
research_cost = 3500;
} else if (tech_type == "biotechnology") {
tech_level = &tech.biotechnology;
tech_name = "生物技术";
research_cost = 2800;
} else if (tech_type == "information") {
tech_level = &tech.information;
tech_name = "信息技术";
research_cost = 2200;
} else if (tech_type == "robotics") {
tech_level = &tech.robotics;
tech_name = "机器人技术";
research_cost = 3200;
} else if (tech_type == "nanotechnology") {
tech_level = &tech.nanotechnology;
tech_name = "纳米技术";
research_cost = 4000;
} else if (tech_type == "quantum") {
tech_level = &tech.quantum;
tech_name = "量子科技";
research_cost = 5000;
}
if (tech_level && *tech_level < max_level) {
if (!tech.canResearch(tech_type)) {
pr("科技前提条件未满足,无法研究该科技\n", 12);
return;
}
double actual_cost = research_cost * (*tech_level + 1);
if (resources.research >= actual_cost) {
resources.research -= actual_cost;
(*tech_level)++;
unlockSpecialTechnologies();
pr((tech_name + "研究成功!当前等级: " + to_string(*tech_level) + "\n").c_str(), 14);
TechEra new_era = tech.getCurrentEra();
if (new_era != getPreviousEra()) {
pr(("科技时代升级!当前时代: " + getEraName(new_era) + "\n").c_str(), 13);
}
} else {
pr("科研点数不足,无法研究科技\n", 12);
}
} else if (tech_level && *tech_level >= max_level) {
pr((tech_name + "已达到最大等级\n").c_str(), 13);
} else {
pr("无效的科技类型\n", 12);
}
}
// 解锁特殊科技
void unlockSpecialTechnologies() {
if (tech.medical >= 5 && !tech.has_advanced_medicine) {
tech.has_advanced_medicine = true;
pr("解锁高级医疗技术!医院效果提升50%\n", 11);
}
if (tech.industrial >= 4 && !tech.has_steel_production) {
tech.has_steel_production = true;
pr("解锁钢铁生产技术!工业效率提升30%\n", 13);
}
if (tech.electronics >= 3 && !tech.has_electricity) {
tech.has_electricity = true;
pr("解锁电力技术!能源生产翻倍\n", 10);
}
if (tech.information >= 5 && !tech.has_computers) {
tech.has_computers = true;
pr("解锁计算机技术!科研效率提升100%\n", 14);
}
if (tech.information >= 8 && !tech.has_internet) {
tech.has_internet = true;
pr("解锁互联网技术!经济和科研全面增强\n", 14);
}
if (tech.robotics >= 6 && !tech.has_ai) {
tech.has_ai = true;
pr("解锁人工智能技术!自动化生产实现\n", 13);
}
if (tech.nuclear >= 8 && !tech.has_fusion_power) {
tech.has_fusion_power = true;
pr("解锁核聚变技术!能源问题彻底解决\n", 11);
}
if (tech.quantum >= 10 && !tech.has_teleportation) {
tech.has_teleportation = true;
pr("解锁量子传送技术!军事机动性大幅提升\n", 13);
}
if (tech.quantum >= 15 && !tech.has_time_travel) {
tech.has_time_travel = true;
pr("解锁时间旅行技术!获得终极力量\n", 11);
}
}
TechEra getPreviousEra() const {
Technology temp_tech = tech;
if (temp_tech.agriculture > 1) temp_tech.agriculture--;
if (temp_tech.military > 1) temp_tech.military--;
if (temp_tech.economic > 1) temp_tech.economic--;
return temp_tech.getCurrentEra();
}
string getEraName(TechEra era) const {
switch(era) {
case TechEra::STONE_AGE: return "石器时代";
case TechEra::BRONZE_AGE: return "青铜时代";
case TechEra::IRON_AGE: return "铁器时代";
case TechEra::CLASSICAL: return "古典时代";
case TechEra::MEDIEVAL: return "中世纪";
case TechEra::RENAISSANCE: return "文艺复兴";
case TechEra::INDUSTRIAL: return "工业时代";
case TechEra::MODERN: return "现代";
case TechEra::INFORMATION: return "信息时代";
case TechEra::DIGITAL: return "数字时代";
case TechEra::BIOTECH: return "生物科技时代";
case TechEra::SPACE_AGE: return "太空时代";
default: return "未知时代";
}
}
// 招募军队
void recruitMilitary(string type, int level, int count) {
MilitaryUnit* unit = military.getUnit(type, level);
if (!unit) {
pr("无效的兵种类型或等级\n", 12);
return;
}
if (level > tech.military) {
pr("军事科技等级不足,无法招募该等级单位\n", 12);
return;
}
// 特殊建筑要求
if (type == "aircraft" && level >= 3 && buildings.airports < level - 2) {
pr("机场等级不足,无法招募该空军单位\n", 12);
return;
}
if (type == "navy" && level >= 3 && buildings.naval_bases < level - 2) {
pr("海军基地等级不足,无法招募该海军单位\n", 12);
return;
}
if (type == "special" && level >= 7 && buildings.ai_cores < 1) {
pr("需要AI核心才能招募高级特殊部队\n", 12);
return;
}
// 计算成本
double cost_multiplier = 1.0;
if (type == "infantry") cost_multiplier = 15;
else if (type == "vehicle") cost_multiplier = 30;
else if (type == "aircraft") cost_multiplier = 100;
else if (type == "navy") cost_multiplier = 80;
else if (type == "artillery") cost_multiplier = 50;
else if (type == "special") cost_multiplier = 40;
double cost = count * cost_multiplier * level * inflation;
double iron_cost = count * level * 3;
double oil_cost = (type == "vehicle" || type == "aircraft" || type == "navy") ? count * level * 2 : 0;
double titanium_cost = (level >= 6) ? count * level : 0;
if (resources.gold >= cost && resources.iron >= iron_cost && resources.oil >= oil_cost && resources.titanium >= titanium_cost) {
military.recruitUnit(type, level, count);
resources.gold -= cost;
resources.iron -= iron_cost;
resources.oil -= oil_cost;
resources.titanium -= titanium_cost;
pr(("成功招募 " + to_string(count) + " 个 " + unit->name + "\n").c_str(), 10);
} else {
pr("资源不足,无法招募部队\n", 12);
}
}
// 资源生产
void produceResources() {
// 基础产出
double gold_income = economy * 200 * (1 + tech.economic * 0.1) * (1 + buildings.factories * 0.05);
double food_production = agriculture * 300 * (1 + tech.agriculture * 0.1) * (1 + buildings.farms * 0.08);
double wood_production = 400 * (1 + buildings.lumber_mills * 0.1);
double iron_production = 200 * (1 + buildings.mines * 0.1);
double oil_production = max(0, tech.industrial - 2) * 100;
double uranium_production = (tech.nuclear >= 1) ? tech.nuclear * 40 : 0;
double titanium_production = (tech.aerospace >= 3) ? tech.aerospace * 30 : 0;
double antimatter_production = (tech.quantum >= 12) ? tech.quantum * 10 : 0;
double research_production = 100 * (1 + buildings.universities * 0.15 + buildings.research_labs * 0.2 + buildings.ai_cores * 0.5);
double energy_production = buildings.power_plants * 200;
double dark_matter_production = (tech.quantum >= 18) ? 5 : 0;
// 科技加成
if (tech.has_electricity) {
gold_income *= 1.3;
research_production *= 1.4;
}
if (tech.has_computers) {
research_production *= 1.8;
gold_income *= 1.5;
}
if (tech.has_internet) {
gold_income *= 1.7;
research_production *= 2.0;
}
if (tech.has_ai) {
gold_income *= 2.0;
research_production *= 3.0;
}
if (tech.has_fusion_power) {
energy_production *= 5.0;
}
// 地形加成
if (terrain == Terrain::FOREST) wood_production *= 2.0;
if (terrain == Terrain::MOUNTAINS) {
iron_production *= 2.5;
titanium_production *= 1.5;
}
if (terrain == Terrain::DESERT) oil_production *= 3.0;
if (terrain == Terrain::PLAINS) food_production *= 1.8;
if (terrain == Terrain::COASTAL) gold_income *= 1.5;
if (terrain == Terrain::URBAN) {
gold_income *= 2.0;
research_production *= 1.8;
}
if (terrain == Terrain::VOLCANIC) {
uranium_production *= 2.0;
energy_production *= 1.5;
}
resources.gold += gold_income;
resources.food += food_production;
resources.wood += wood_production;
resources.iron += iron_production;
resources.oil += oil_production;
resources.uranium += uranium_production;
resources.titanium += titanium_production;
resources.antimatter += antimatter_production;
resources.research += research_production;
resources.energy += energy_production;
resources.dark_matter += dark_matter_production;
// 通货膨胀调整
resources.gold *= (1 - inflation * 0.01);
// 人口消耗和增长
double food_consumption = population * 0.002;
double energy_consumption = population * 0.001 + military.totalPower() * 0.02;
resources.food -= food_consumption;
resources.energy -= energy_consumption;
// 人口增长
if (resources.food > 0 && health > 60 && resources.energy > 0) {
double growth_rate = min(health / 150.0, resources.food / (population * 0.005));
growth_rate *= (1 + tech.medical * 0.08);
population += static_cast<int>(population * growth_rate * 0.015);
} else if (resources.food < 0 || health < 40 || resources.energy < 0) {
double decline_rate = 0.08;
population -= static_cast<int>(population * decline_rate);
stability -= 15;
happiness -= 20;
}
// 建筑维护费用
double maintenance_cost = (buildings.farms + buildings.mines + buildings.lumber_mills +
buildings.barracks + buildings.universities + buildings.hospitals +
buildings.factories + buildings.power_plants + buildings.research_labs +
buildings.military_academy + buildings.airports + buildings.naval_bases +
buildings.space_ports + buildings.ai_cores + buildings.quantum_computers) * 100;
resources.gold -= maintenance_cost;
// 军队维护费用
double military_maintenance = military.totalMaintenance() * inflation;
resources.gold -= military_maintenance;
// 全球影响力增长
global_influence += (economy + military.totalPower() / 10000 + tech.military) * 0.001;
// 幸福度和稳定度更新
updateHappinessAndStability();
turn_count++;
}
// 更新幸福度和稳定度
void updateHappinessAndStability() {
double base_happiness = 60.0;
double base_stability = 65.0;
// 食物影响
double food_ratio = resources.food / (population * 0.005);
base_happiness += min(30.0, food_ratio * 15);
base_stability += min(25.0, food_ratio * 10);
// 健康影响
base_happiness += (health - 60) * 0.4;
base_stability += (health - 60) * 0.3;
// 经济影响
base_happiness += (economy - 60) * 0.3;
base_stability += (economy - 60) * 0.4;
// 教育影响
base_happiness += (education - 50) * 0.2;
base_stability += (education - 50) * 0.2;
// 能源影响
if (resources.energy > 0) {
base_happiness += 15;
} else {
base_happiness -= 25;
base_stability -= 20;
}
// 通货膨胀影响
base_happiness -= inflation * 10;
base_stability -= inflation * 8;
// 军事压力
double military_pressure = military.totalPower() / 50000.0;
base_happiness -= min(25.0, military_pressure);
// 建筑提供幸福度
base_happiness += buildings.hospitals * 5;
base_happiness += buildings.universities * 8;
base_happiness += buildings.power_plants * 7.5;
happiness = max(0.0, min(200.0, base_happiness));
stability = max(0.0, min(200.0, base_stability));
}
// 显示详细状态
void displayStatus() const {
TechEra current_era = tech.getCurrentEra();
cout << "\n╔════════════════════════════════════════════════════════════╗" << endl;
cout << "║ " << name << " - " << leader_name << " ║" << endl;
cout << "║ " << getEraName(current_era) << " ║" << endl;
cout << "╠════════════════════════════════════════════════════════════╣" << endl;
// 基础信息
cout << "║ 地形: ";
switch(terrain) {
case Terrain::PLAINS: cout << "平原 "; break;
case Terrain::MOUNTAINS: cout << "山脉 "; break;
case Terrain::FOREST: cout << "森林 "; break;
case Terrain::DESERT: cout << "沙漠 "; break;
case Terrain::COASTAL: cout << "沿海 "; break;
case Terrain::ARCTIC: cout << "极地 "; break;
case Terrain::URBAN: cout << "城市 "; break;
case Terrain::VOLCANIC: cout << "火山 "; break;
}
cout << "人口: " << setw(15) << population << " ║" << endl;
// 国家指标
cout << "║ 经济: " << setw(5) << static_cast<int>(economy) << " 农业: " << setw(5) << static_cast<int>(agriculture);
cout << " 健康: " << setw(5) << static_cast<int>(health) << " 稳定: " << setw(5) << static_cast<int>(stability) << " ║" << endl;
cout << "║ 幸福: " << setw(5) << static_cast<int>(happiness) << " 教育: " << setw(5) << static_cast<int>(education);
cout << " 通胀: " << fixed << setprecision(1) << inflation << "% 影响力: " << setw(6) << static_cast<int>(global_influence) << " ║" << endl;
cout << "║ 领土: " << setw(6) << territory_size << " km2 回合: " << setw(4) << turn_count;
cout << " 战力: " << setw(12) << static_cast<int>(military.totalPower()) << " ║" << endl;
cout << "╠════════════════════════════════════════════════════════════╣" << endl;
cout << "║ 资源储备 ║" << endl;
cout << "║ 黄金: " << setw(10) << static_cast<int>(resources.gold) << " 食物: " << setw(10) << static_cast<int>(resources.food);
cout << " 木材: " << setw(10) << static_cast<int>(resources.wood) << " 铁矿: " << setw(10) << static_cast<int>(resources.iron) << " ║" << endl;
cout << "║ 石油: " << setw(10) << static_cast<int>(resources.oil) << " 铀矿: " << setw(10) << static_cast<int>(resources.uranium);
cout << " 钛矿: " << setw(10) << static_cast<int>(resources.titanium) << " 能源: " << setw(10) << static_cast<int>(resources.energy) << " ║" << endl;
cout << "║ 反物质: " << setw(8) << static_cast<int>(resources.antimatter) << " 暗物质: " << setw(8) << static_cast<int>(resources.dark_matter);
cout << " 科研: " << setw(10) << static_cast<int>(resources.research) << " ║" << endl;
cout << "╠════════════════════════════════════════════════════════════╣" << endl;
cout << "║ 科技水平 ║" << endl;
cout << "║ 农业: " << setw(2) << tech.agriculture << " 军事: " << setw(2) << tech.military;
cout << " 经济: " << setw(2) << tech.economic << " 医疗: " << setw(2) << tech.medical;
cout << " 工业: " << setw(2) << tech.industrial << " 建筑: " << setw(2) << tech.construction << " ║" << endl;
cout << "║ 电子: " << setw(2) << tech.electronics << " 航天: " << setw(2) << tech.aerospace;
cout << " 核能: " << setw(2) << tech.nuclear << " 生物: " << setw(2) << tech.biotechnology;
cout << " 信息: " << setw(2) << tech.information << " 机器人: " << setw(2) << tech.robotics << " ║" << endl;
cout << "║ 纳米: " << setw(2) << tech.nanotechnology << " 量子: " << setw(2) << tech.quantum;
cout << " ";
if (tech.has_time_travel) cout << "时间旅行";
else if (tech.has_teleportation) cout << "量子传送";
else if (tech.has_fusion_power) cout << "核聚变";
else if (tech.has_ai) cout << "人工智能";
else cout << " ";
cout << " ║" << endl;
cout << "╚════════════════════════════════════════════════════════════╝" << endl;
}
// 显示外交关系
void displayDiplomacy() const {
pr("\n=== 外交关系 ===\n", 11);
for (const auto& relation : diplomacy) {
cout << relation.first << ": ";
switch(relation.second) {
case Relation::ALLY: pr("同盟\n", 10); break;
case Relation::NEUTRAL: pr("中立\n", 7); break;
case Relation::ENEMY: pr("敌国\n", 12); break;
case Relation::VASSAL: pr("附庸\n", 13); break;
case Relation::TRADE_PARTNER: pr("贸易伙伴\n", 14); break;
}
}
}
// 检查是否灭亡
bool isDefeated() const {
return stability <= 0 || population <= 200000 || health <= 0 || happiness <= 0;
}
// 获取回合数
int getTurnCount() const { return turn_count; }
// 获取当前科技时代
TechEra getTechEra() const {
return tech.getCurrentEra();
}
// 为存档系统添加的getter和setter方法
double getAgriculture() const { return agriculture; }
double getHealth() const { return health; }
double getStability() const { return stability; }
double getHappiness() const { return happiness; }
double getEducation() const { return education; }
int getTerritorySize() const { return territory_size; }
double getInflation() const { return inflation; }
void setAgriculture(double value) { agriculture = value; }
void setHealth(double value) { health = value; }
void setStability(double value) { stability = value; }
void setHappiness(double value) { happiness = value; }
void setEducation(double value) { education = value; }
void setPopulation(int value) { population = value; }
void setTerritorySize(int value) { territory_size = value; }
void setInflation(double value) { inflation = value; }
void setTurnCount(int value) { turn_count = value; }
void setGlobalInfluence(double value) { global_influence = value; }
// 获取军事单位的完整信息(用于存档)
const vector<MilitaryUnit>& getMilitaryUnits() const {
return military.getAllUnits();
}
// 设置军事单位数量(用于读档)
void setMilitaryUnitCount(const string& type, int level, int count) {
military.setUnitCount(type, level, count);
}
};
// 战斗系统
class BattleSystem {
public:
static double getTerrainBonus(Terrain attacker, Terrain defender, const MilitaryForces& attacker_mil) {
double bonus = 1.0;
if (attacker == Terrain::MOUNTAINS && defender == Terrain::PLAINS) bonus *= 1.4;
if (attacker == Terrain::PLAINS && defender == Terrain::FOREST) bonus *= 0.7;
if (attacker == Terrain::FOREST && defender == Terrain::PLAINS) bonus *= 1.3;
if (attacker == Terrain::DESERT && defender == Terrain::COASTAL) bonus *= 1.2;
if (attacker == Terrain::COASTAL && defender != Terrain::COASTAL) bonus *= 1.2;
if (attacker == Terrain::URBAN) bonus *= 1.3;
if (attacker == Terrain::VOLCANIC) bonus *= 1.1;
return bonus;
}
static bool conductBattle(Nation& attacker, Nation& defender) {
pr(("\n" + attacker.getName() + " 向 " + defender.getName() + " 宣战!\n").c_str(), 12);
double attackerPower = attacker.getMilitary().totalPower();
double defenderPower = defender.getMilitary().totalPower();
double terrainBonus = getTerrainBonus(attacker.getTerrain(), defender.getTerrain(), attacker.getMilitary());
attackerPower *= terrainBonus;
double techBonus = 1.0 + (attacker.getTechnology().military - defender.getTechnology().military) * 0.15;
attackerPower *= techBonus;
uniform_real_distribution<double> dist(0.7, 1.3);
attackerPower *= dist(global_rng);
defenderPower *= dist(global_rng);
cout << "攻击方战力: " << attackerPower << " (地形加成: " << fixed << setprecision(1) << terrainBonus;
cout << ", 科技加成: " << techBonus << ")" << endl;
cout << "防御方战力: " << defenderPower << endl;
bool attackerWins = attackerPower > defenderPower;
if (attackerWins) {
pr("攻击方获得胜利!\n", 10);
// 胜利奖励
double dels=1.0/((rand()%50)*1.0+0.01);
attacker.getResources().gold += defender.getResources().gold*dels;
attacker.getResources().food += 6000;
attacker.getResources().iron += 1000;
attacker.getResources().oil += defender.getResources().oil*0.1;
attacker.getResources().research += 1000;
// 失败惩罚
defender.getResources().gold *=(1-dels);
defender.getResources().food *= 0.6;
defender.getResources().iron *= 0.6;
defender.getResources().oil*=0.9;
} else {
pr("防御方获得胜利!\n", 11);
double dels=1.0/((rand()%50)*1.0);
// 防御方获得奖励
defender.getResources().gold += attacker.getResources().gold*dels;
defender.getResources().iron += 400;
defender.getResources().research += 500;
attacker.getResources().gold*=0.98;
}
return attackerWins;
}
};
// 全球广播系统
class GlobalBroadcast {
public:
static void broadcastWorldStatus(const vector<shared_ptr<Nation>>& nations, int turn) {
pr("\n════════════════════════════ 全球局势报告 - 第 ", 14);
cout << turn << " 回合 ";
pr("════════════════════════════\n", 14);
cout << left << setw(20) << "国家" << setw(15) << "时代"
<< setw(12) << "人口" << setw(12) << "经济" << setw(12) << "军事"
<< setw(12) << "科技" << setw(15) << "全球影响力" << setw(10) << "状态" << endl;
vector<tuple<double, string, TechEra, int, double, double, int, double>> nation_data;
for (const auto& nation : nations) {
if (!nation->isDefeated()) {
double score = nation->getEconomy() + nation->getMilitary().totalPower() / 10000 +
nation->getGlobalInfluence() + nation->getPopulation() / 100000.0;
nation_data.emplace_back(score, nation->getName(), nation->getTechEra(),
nation->getPopulation(), nation->getEconomy(),
nation->getMilitary().totalPower(), nation->getTechnology().military,
nation->getGlobalInfluence());
}
}
// 按分数排序
sort(nation_data.rbegin(), nation_data.rend());
for (const auto& data : nation_data) {
double score;
string name;
TechEra era;
int pop;
double economy, military;
int tech;
double influence;
tie(score, name, era, pop, economy, tech, military, influence) = data;
cout << left << setw(20) << name
<< setw(15) << getEraName(era)
<< setw(12) << (pop > 1000000 ? to_string(pop/1000000) + "M" : to_string(pop/1000) + "K")
<< setw(12) << static_cast<int>(economy)
<< setw(12) << (military > 1000000 ? to_string(static_cast<int>(military)/1000000) + "M" :
military > 1000 ? to_string(static_cast<int>(military)/1000) + "K" : to_string(static_cast<int>(military)))
<< setw(12) << tech
<< setw(15) << fixed << setprecision(1) << influence
<< setw(10) << "繁荣" << endl;
}
// 显示灭亡国家
bool has_defeated = false;
for (const auto& nation : nations) {
if (nation->isDefeated()) {
if (!has_defeated) {
pr("\n已灭亡国家:\n", 12);
has_defeated = true;
}
cout << " " << nation->getName() << endl;
}
}
pr("\n══════════════════════════════════════════════════════════════════════════════════\n", 14);
}
private:
static string getEraName(TechEra era) {
switch(era) {
case TechEra::STONE_AGE: return "石器时代";
case TechEra::BRONZE_AGE: return "青铜时代";
case TechEra::IRON_AGE: return "铁器时代";
case TechEra::CLASSICAL: return "古典时代";
case TechEra::MEDIEVAL: return "中世纪";
case TechEra::RENAISSANCE: return "文艺复兴";
case TechEra::INDUSTRIAL: return "工业时代";
case TechEra::MODERN: return "现代";
case TechEra::INFORMATION: return "信息时代";
case TechEra::DIGITAL: return "数字时代";
case TechEra::BIOTECH: return "生物科技";
case TechEra::SPACE_AGE: return "太空时代";
default: return "未知";
}
}
};
// 增强的智能AI系统
class AIStrategy {
private:
shared_ptr<Nation> nation;
int aggression_level; // 0-10, 侵略性等级
int focus_type; // 0:平衡, 1:军事, 2:经济, 3:科技
public:
AIStrategy(shared_ptr<Nation> n) : nation(n) {
// 随机生成AI性格
uniform_int_distribution<int> agg_dist(0, 10);
uniform_int_distribution<int> focus_dist(0, 3);
aggression_level = agg_dist(global_rng);
focus_type = focus_dist(global_rng);
}
void executeTurn() {
TechEra current_era = nation->getTechEra();
double military_power = nation->getMilitary().totalPower();
double gold = nation->getResources().gold;
double research = nation->getResources().research;
double food = nation->getResources().food;
// 紧急情况处理
if (handleEmergencies()) {
return;
}
// 根据AI性格和当前状况制定策略
if (current_era <= TechEra::MEDIEVAL) {
executeAncientStrategy();
} else if (current_era <= TechEra::INDUSTRIAL) {
executeIndustrialStrategy();
} else if (current_era <= TechEra::INFORMATION) {
executeModernStrategy();
} else {
executeFutureStrategy();
}
// 根据侵略性决定是否宣战
if (shouldDeclareWar()) {
declareWar();
}
}
private:
bool handleEmergencies() {
// 处理紧急情况
if (nation->getResources().food < 5000) {
nation->developAgriculture();
return true;
}
if (nation->getResources().gold < 2000) {
nation->developEconomy();
return true;
}
if (nation->getStability() < 40) {
if (nation->getHealth() < 60) {
nation->developHealth();
} else {
nation->developEconomy();
}
return true;
}
return false;
}
void executeAncientStrategy() {
// 古代策略:重点发展基础资源
double priority_food = (nation->getResources().food < 15000) ? 2.0 : 0.5;
double priority_gold = (nation->getResources().gold < 8000) ? 1.5 : 0.7;
double priority_military = (aggression_level > 7) ? 1.8 : 0.8;
vector<pair<double, string>> actions = {
{priority_food, "agriculture"},
{priority_gold, "economy"},
{priority_military, "military"},
{1.0, "industry"},
{0.8, "health"}
};
// 根据优先级排序
sort(actions.rbegin(), actions.rend());
// 执行最高优先级行动
string best_action = actions[0].second;
executeAction(best_action);
}
void executeIndustrialStrategy() {
// 工业时代策略:平衡发展
vector<pair<double, string>> actions;
// 根据AI专注类型调整权重
double military_weight = (focus_type == 1) ? 2.0 : 1.0;
double economy_weight = (focus_type == 2) ? 2.0 : 1.0;
double tech_weight = (focus_type == 3) ? 2.0 : 1.0;
actions.emplace_back(military_weight * evaluateMilitaryNeed(), "military");
actions.emplace_back(economy_weight * evaluateEconomyNeed(), "economy");
actions.emplace_back(tech_weight * evaluateTechNeed(), getBestTechToResearch());
actions.emplace_back(1.0, "industry");
actions.emplace_back(0.8, "agriculture");
sort(actions.rbegin(), actions.rend());
executeAction(actions[0].second);
}
void executeModernStrategy() {
// 现代策略:科技优先,但考虑军事平衡
vector<pair<double, string>> actions;
double military_need = evaluateMilitaryNeed();
if (military_need > 0.7) {
actions.emplace_back(2.0, "military");
}
actions.emplace_back(1.5, getBestTechToResearch());
actions.emplace_back(1.2, "economy");
actions.emplace_back(1.0, "industry");
sort(actions.rbegin(), actions.rend());
executeAction(actions[0].second);
}
void executeFutureStrategy() {
// 未来策略:追求终极科技,但保持军事优势
vector<pair<double, string>> actions;
// 检查是否需要加强军事
if (evaluateMilitaryNeed() > 0.6) {
actions.emplace_back(1.8, "military");
}
// 优先研究高级科技
actions.emplace_back(2.0, getBestFutureTech());
actions.emplace_back(1.3, "economy");
sort(actions.rbegin(), actions.rend());
executeAction(actions[0].second);
}
double evaluateMilitaryNeed() {
// 评估军事需求
double current_power = nation->getMilitary().totalPower();
double expected_threat = aggression_level * 50000;
// 检查邻国军事力量
double neighbor_threat = 0;
// 这里可以添加检查邻国军事力量的逻辑
double total_threat = expected_threat + neighbor_threat;
if (current_power < total_threat * 0.7) {
return 1.0; // 急需加强军事
} else if (current_power < total_threat) {
return 0.6; // 需要加强军事
} else {
return 0.3; // 军事足够
}
}
double evaluateEconomyNeed() {
double gold = nation->getResources().gold;
if (gold < 5000) return 1.0;
if (gold < 15000) return 0.7;
return 0.3;
}
double evaluateTechNeed() {
TechEra era = nation->getTechEra();
if (era <= TechEra::MEDIEVAL) return 0.8;
if (era <= TechEra::INDUSTRIAL) return 1.0;
if (era <= TechEra::MODERN) return 1.2;
return 1.5;
}
string getBestTechToResearch() {
TechEra era = nation->getTechEra();
const Technology& tech = nation->getTechnology();
if (era <= TechEra::MEDIEVAL) {
if (tech.agriculture < 4) return "agriculture";
if (tech.military < 3) return "military";
return "economic";
} else if (era <= TechEra::INDUSTRIAL) {
if (tech.industrial < 6) return "industrial";
if (tech.electronics < 3) return "electronics";
return "military";
} else if (era <= TechEra::MODERN) {
if (tech.information < 5) return "information";
if (tech.aerospace < 4) return "aerospace";
return "robotics";
} else {
if (tech.quantum < 8) return "quantum";
if (tech.nanotechnology < 6) return "nanotechnology";
return "biotechnology";
}
}
string getBestFutureTech() {
const Technology& tech = nation->getTechnology();
if (tech.quantum < tech.nanotechnology) return "quantum";
if (tech.nanotechnology < tech.robotics) return "nanotechnology";
return "robotics";
}
void executeAction(const string& action) {
if (action == "agriculture") {
nation->developAgriculture();
} else if (action == "economy") {
nation->developEconomy();
} else if (action == "military") {
// 招募军队
recruitMilitary();
} else if (action == "industry") {
nation->developIndustry();
} else if (action == "health") {
nation->developHealth();
} else {
// 研究科技
nation->researchTechnology(action);
}
}
void recruitMilitary() {
int max_level = nation->getTechnology().military;
vector<string> unit_types = {"infantry", "vehicle"};
if (max_level >= 3) unit_types.push_back("artillery");
if (max_level >= 4) unit_types.push_back("aircraft");
if (max_level >= 5) unit_types.push_back("navy");
if (max_level >= 6) unit_types.push_back("special");
uniform_int_distribution<int> type_dist(0, unit_types.size() - 1);
uniform_int_distribution<int> level_dist(1, min(max_level, 5));
uniform_int_distribution<int> count_dist(50, 200);
string type = unit_types[type_dist(global_rng)];
int level = level_dist(global_rng);
int count = count_dist(global_rng);
// 根据经济状况调整招募数量
double gold = nation->getResources().gold;
if (gold < 10000) count = count_dist(global_rng) / 2;
nation->recruitMilitary(type, level, count);
}
bool shouldDeclareWar() {
if (aggression_level < 5) return false;
// 检查军事优势
double my_power = nation->getMilitary().totalPower();
// 这里可以添加检查潜在目标军事力量的逻辑
// 简化:随机决定
uniform_int_distribution<int> war_chance(0, 100);
return war_chance(global_rng) < (aggression_level * 3);
}
void declareWar() {
// 这里可以添加选择战争目标的逻辑
pr("AI考虑发动战争\n", 12);
}
};
// 游戏主类
class Game {
private:
vector<shared_ptr<Nation>> nations;
int currentPlayer;
int totalTurns;
bool gameRunning;
string saveFileName;
public:
Game() : currentPlayer(0), totalTurns(0), gameRunning(true), saveFileName("game_save.dat") {
initializeNations();
setupInitialDiplomacy();
}
void run() {
displayWelcome();
while (gameRunning) {
auto& currentNation = nations[currentPlayer];
if (!currentNation->isDefeated()) {
displayTurnHeader(currentNation);
currentNation->displayStatus();
if (currentPlayer == 0) { // 玩家回合
playerTurn(currentNation);
} else { // AI回合
aiTurn(currentNation);
}
// 回合结束处理
currentNation->produceResources();
// 检查游戏结束
if (checkGameEnd()) {
break;
}
}
// 切换到下一个玩家
currentPlayer = (currentPlayer + 1) % nations.size();
if (currentPlayer == 0) {
totalTurns++;
// 每回合全球广播
GlobalBroadcast::broadcastWorldStatus(nations, totalTurns);
}
}
displayGameResult();
}
// 存档功能
void saveGame() {
ofstream file(saveFileName, ios::binary);
if (!file) {
pr("无法创建存档文件!\n", 12);
return;
}
// 保存游戏基础信息
file.write(reinterpret_cast<const char*>(&totalTurns), sizeof(totalTurns));
file.write(reinterpret_cast<const char*>(¤tPlayer), sizeof(currentPlayer));
// 保存国家数量
int nationCount = nations.size();
file.write(reinterpret_cast<const char*>(&nationCount), sizeof(nationCount));
// 保存每个国家的数据
for (const auto& nation : nations) {
saveNation(file, nation);
}
file.close();
pr("游戏已成功存档!\n", 10);
}
// 读档功能
bool loadGame() {
ifstream file(saveFileName, ios::binary);
if (!file) {
pr("找不到存档文件!\n", 12);
return false;
}
// 读取游戏基础信息
file.read(reinterpret_cast<char*>(&totalTurns), sizeof(totalTurns));
file.read(reinterpret_cast<char*>(¤tPlayer), sizeof(currentPlayer));
// 读取国家数量
int nationCount;
file.read(reinterpret_cast<char*>(&nationCount), sizeof(nationCount));
// 清空当前国家列表
nations.clear();
// 读取每个国家的数据
for (int i = 0; i < nationCount; i++) {
auto nation = loadNation(file);
if (nation) {
nations.push_back(nation);
}
}
file.close();
pr("游戏已成功读档!\n", 10);
return true;
}
private:
void displayWelcome() {
pr("╔══════════════════════════════════════════════════════════════════════════════════╗\n", 14);
pr("║ 国家治理与战争模拟器 - 终极版 ║\n", 14);
pr("║ Nation Governance & War Simulator - Ultimate ║\n", 14);
pr("╚══════════════════════════════════════════════════════════════════════════════════╝\n", 14);
pr("欢迎来到终极国家模拟游戏!管理你的国家,从石器时代发展到太空时代,征服世界!\n", 11);
pr("游戏特性:\n", 13);
pr("? 12个科技时代,从石器时代到太空时代\n", 10);
pr("? 14种科技分支,超过200个科技等级\n", 10);
pr("? 6种军事单位类型,每个类型10个等级\n", 10);
pr("? 15种建筑类型,无限升级\n", 10);
pr("? 11种资源类型,包括反物质和暗物质\n", 10);
pr("? 智能AI对手,每个都有独特策略\n", 10);
pr("? 每回合全球局势报告\n", 10);
pr("? 完整的存档和读档系统\n", 10);
pr("? 无限制回合数,无限可能性\n", 10);
pr("\n按回车键开始游戏...", 7);
cin.get();
}
void displayTurnHeader(shared_ptr<Nation> nation) {
pr(("\n════════════════════════════ 第 " + to_string(totalTurns + 1) + " 回合 ════════════════════════════\n").c_str(), 11);
if (currentPlayer == 0) {
pr(("当前控制: " + nation->getName() + " - " + nation->getLeaderName() + "\n").c_str(), 11);
} else {
pr(("AI回合: " + nation->getName() + " - " + nation->getLeaderName() + "\n").c_str(), 13);
}
pr(("科技时代: " + nation->getEraName(nation->getTechEra()) + "\n").c_str(), 14);
}
void initializeNations() {
// 创建12个具有不同特色的国家
nations.push_back(make_shared<Nation>("华夏帝国", "龙皇", Terrain::PLAINS));
nations.push_back(make_shared<Nation>("北欧王国", "雷神", Terrain::MOUNTAINS));
nations.push_back(make_shared<Nation>("森林联邦", "精灵王", Terrain::FOREST));
nations.push_back(make_shared<Nation>("沙漠王朝", "太阳王", Terrain::DESERT));
nations.push_back(make_shared<Nation>("海洋共和国", "海皇", Terrain::COASTAL));
nations.push_back(make_shared<Nation>("冰原部落", "冰雪女王", Terrain::ARCTIC));
nations.push_back(make_shared<Nation>("未来都市", "AI执政官", Terrain::URBAN));
nations.push_back(make_shared<Nation>("火山帝国", "熔岩领主", Terrain::VOLCANIC));
nations.push_back(make_shared<Nation>("东方王朝", "天子", Terrain::PLAINS));
nations.push_back(make_shared<Nation>("西部联邦", "大酋长", Terrain::FOREST));
nations.push_back(make_shared<Nation>("南方帝国", "法老", Terrain::DESERT));
nations.push_back(make_shared<Nation>("北方王国", "冰霜巨人", Terrain::ARCTIC));
}
void setupInitialDiplomacy() {
// 设置复杂的外交关系网
uniform_int_distribution<int> dist_diplo(0, 2);
uniform_int_distribution<int> dist_diplo2(0, 3);
for (int i = 0; i < nations.size(); i++) {
for (int j = i + 1; j < nations.size(); j++) {
if (dist_diplo(global_rng) == 0) {
nations[i]->setRelation(nations[j]->getName(), Relation::ENEMY);
nations[j]->setRelation(nations[i]->getName(), Relation::ENEMY);
} else if (dist_diplo2(global_rng) == 0) {
nations[i]->setRelation(nations[j]->getName(), Relation::ALLY);
nations[j]->setRelation(nations[i]->getName(), Relation::ALLY);
} else {
nations[i]->setRelation(nations[j]->getName(), Relation::NEUTRAL);
nations[j]->setRelation(nations[i]->getName(), Relation::NEUTRAL);
}
}
}
}
void playerTurn(shared_ptr<Nation> nation) {
int choice;
do {
cout << "\n═══════════════ 行动菜单 ═══════════════" << endl;
cout << " 1. 发展经济 2. 发展农业 3. 发展医疗" << endl;
cout << " 4. 发展工业 5. 发展教育 6. 升级建筑" << endl;
cout << " 7. 研究科技 8. 招募军队 9. 升级军队" << endl;
cout << "10. 发动战争 11. 外交关系 12. 军事详情" << endl;
cout << "13. 保存游戏 14. 加载游戏 15. 结束回合" << endl;
cout << "═══════════════════════════════════════════" << endl;
cout << "选择行动: ";
cin >> choice;
switch(choice) {
case 1:
nation->developEconomy();
break;
case 2:
nation->developAgriculture();
break;
case 3:
nation->developHealth();
break;
case 4:
nation->developIndustry();
break;
case 5:
nation->developEducation();
break;
case 6:
buildingMenu(nation);
break;
case 7:
researchMenu(nation);
break;
case 8:
recruitMenu(nation);
break;
case 9:
upgradeMilitaryMenu(nation);
break;
case 10:
declareWar(nation);
break;
case 11:
nation->displayDiplomacy();
break;
case 12:
nation->getMilitary().displayForces();
break;
case 13:
saveGame();
break;
case 14:
if (loadGame()) {
pr("读档成功!游戏状态已恢复。\n", 10);
return; // 结束当前回合,因为游戏状态已改变
}
break;
case 15:
pr("结束回合\n", 13);
break;
default:
pr("无效选择\n", 12);
}
} while (choice != 15);
}
void buildingMenu(shared_ptr<Nation> nation) {
cout << "\n════════════ 建筑升级 ════════════" << endl;
cout << " 1. 农场 (等级: " << nation->getBuildings().farms << ")" << endl;
cout << " 2. 矿场 (等级: " << nation->getBuildings().mines << ")" << endl;
cout << " 3. 伐木场 (等级: " << nation->getBuildings().lumber_mills << ")" << endl;
cout << " 4. 兵营 (等级: " << nation->getBuildings().barracks << ")" << endl;
cout << " 5. 大学 (等级: " << nation->getBuildings().universities << ")" << endl;
cout << " 6. 医院 (等级: " << nation->getBuildings().hospitals << ")" << endl;
cout << " 7. 工厂 (等级: " << nation->getBuildings().factories << ")" << endl;
cout << " 8. 发电厂 (等级: " << nation->getBuildings().power_plants << ")" << endl;
cout << " 9. 研究所 (等级: " << nation->getBuildings().research_labs << ")" << endl;
cout << "10. 军事学院 (等级: " << nation->getBuildings().military_academy << ")" << endl;
cout << "11. 机场 (等级: " << nation->getBuildings().airports << ")" << endl;
cout << "12. 海军基地 (等级: " << nation->getBuildings().naval_bases << ")" << endl;
cout << "13. 太空港 (等级: " << nation->getBuildings().space_ports << ")" << endl;
cout << "14. AI核心 (等级: " << nation->getBuildings().ai_cores << ")" << endl;
cout << "15. 量子计算机 (等级: " << nation->getBuildings().quantum_computers << ")" << endl;
cout << "16. 返回" << endl;
cout << "选择要升级的建筑: ";
int choice;
cin >> choice;
switch(choice) {
case 1: nation->upgradeBuilding("farm"); break;
case 2: nation->upgradeBuilding("mine"); break;
case 3: nation->upgradeBuilding("lumber"); break;
case 4: nation->upgradeBuilding("barracks"); break;
case 5: nation->upgradeBuilding("university"); break;
case 6: nation->upgradeBuilding("hospital"); break;
case 7: nation->upgradeBuilding("factory"); break;
case 8: nation->upgradeBuilding("power_plant"); break;
case 9: nation->upgradeBuilding("research_lab"); break;
case 10: nation->upgradeBuilding("military_academy"); break;
case 11: nation->upgradeBuilding("airport"); break;
case 12: nation->upgradeBuilding("naval_base"); break;
case 13: nation->upgradeBuilding("space_port"); break;
case 14: nation->upgradeBuilding("ai_core"); break;
case 15: nation->upgradeBuilding("quantum_computer"); break;
case 16: break;
default: pr("无效选择\n", 12);
}
}
void researchMenu(shared_ptr<Nation> nation) {
TechEra current_era = nation->getTechEra();
cout << "\n════════════ 科技研究 ════════════" << endl;
cout << "当前科技时代: " << nation->getEraName(current_era) << endl;
cout << "基础科技:" << endl;
cout << " 1. 农业科技 (等级: " << nation->getTechnology().agriculture << "/20)" << endl;
cout << " 2. 军事科技 (等级: " << nation->getTechnology().military << "/20)" << endl;
cout << " 3. 经济科技 (等级: " << nation->getTechnology().economic << "/20)" << endl;
cout << " 4. 医疗科技 (等级: " << nation->getTechnology().medical << "/20)" << endl;
cout << " 5. 工业科技 (等级: " << nation->getTechnology().industrial << "/20)" << endl;
cout << " 6. 建筑科技 (等级: " << nation->getTechnology().construction << "/20)" << endl;
if (current_era >= TechEra::INDUSTRIAL) {
cout << "工业科技:" << endl;
cout << " 7. 电子科技 (等级: " << nation->getTechnology().electronics << "/20)" << endl;
cout << " 8. 航空航天 (等级: " << nation->getTechnology().aerospace << "/20)" << endl;
cout << " 9. 核能科技 (等级: " << nation->getTechnology().nuclear << "/20)" << endl;
}
if (current_era >= TechEra::MODERN) {
cout << "现代科技:" << endl;
cout << "10. 生物技术 (等级: " << nation->getTechnology().biotechnology << "/20)" << endl;
cout << "11. 信息技术 (等级: " << nation->getTechnology().information << "/20)" << endl;
cout << "12. 机器人技术 (等级: " << nation->getTechnology().robotics << "/20)" << endl;
}
if (current_era >= TechEra::DIGITAL) {
cout << "未来科技:" << endl;
cout << "13. 纳米技术 (等级: " << nation->getTechnology().nanotechnology << "/20)" << endl;
cout << "14. 量子科技 (等级: " << nation->getTechnology().quantum << "/20)" << endl;
}
cout << "15. 返回" << endl;
cout << "选择要研究的科技: ";
int choice;
cin >> choice;
switch(choice) {
case 1: nation->researchTechnology("agriculture"); break;
case 2: nation->researchTechnology("military"); break;
case 3: nation->researchTechnology("economic"); break;
case 4: nation->researchTechnology("medical"); break;
case 5: nation->researchTechnology("industrial"); break;
case 6: nation->researchTechnology("construction"); break;
case 7:
if (current_era >= TechEra::INDUSTRIAL) nation->researchTechnology("electronics");
else pr("尚未达到工业时代,无法研究该科技\n", 12);
break;
case 8:
if (current_era >= TechEra::INDUSTRIAL) nation->researchTechnology("aerospace");
else pr("尚未达到工业时代,无法研究该科技\n", 12);
break;
case 9:
if (current_era >= TechEra::INDUSTRIAL) nation->researchTechnology("nuclear");
else pr("尚未达到工业时代,无法研究该科技\n", 12);
break;
case 10:
if (current_era >= TechEra::MODERN) nation->researchTechnology("biotechnology");
else pr("尚未达到现代,无法研究该科技\n", 12);
break;
case 11:
if (current_era >= TechEra::MODERN) nation->researchTechnology("information");
else pr("尚未达到现代,无法研究该科技\n", 12);
break;
case 12:
if (current_era >= TechEra::MODERN) nation->researchTechnology("robotics");
else pr("尚未达到现代,无法研究该科技\n", 12);
break;
case 13:
if (current_era >= TechEra::DIGITAL) nation->researchTechnology("nanotechnology");
else pr("尚未达到数字时代,无法研究该科技\n", 12);
break;
case 14:
if (current_era >= TechEra::DIGITAL) nation->researchTechnology("quantum");
else pr("尚未达到数字时代,无法研究该科技\n", 12);
break;
case 15: break;
default: pr("无效选择\n", 12);
}
}
void recruitMenu(shared_ptr<Nation> nation) {
int max_tech_level = nation->getTechnology().military;
cout << "\n════════════ 军队招募 ════════════" << endl;
cout << "当前军事科技等级: " << max_tech_level << " (最高可招募等级 " << max_tech_level << " 单位)" << endl;
auto available_units = nation->getMilitary().getAvailableUnits(max_tech_level);
map<string, vector<MilitaryUnit*>> units_by_type;
for (auto unit : available_units) {
units_by_type[unit->type].push_back(unit);
}
int index = 1;
map<int, pair<string, int>> unit_map;
for (const auto& type_pair : units_by_type) {
const string& type = type_pair.first;
const vector<MilitaryUnit*>& type_units = type_pair.second;
string type_name;
if (type == "infantry") type_name = "步兵";
else if (type == "vehicle") type_name = "车辆";
else if (type == "aircraft") type_name = "空军";
else if (type == "navy") type_name = "海军";
else if (type == "special") type_name = "特殊部队";
cout << "\n" << type_name << ":\n";
for (const auto unit : type_units) {
cout << " " << index << ". " << unit->name << " (等级 " << unit->level << ")";
cout << " | 攻击: " << unit->attack_power << " | 防御: " << unit->defense_power;
cout << " | 移动: " << unit->mobility << " | 成本: " << (15 * unit->level) << "黄金\n";
unit_map[index] = make_pair(unit->type, unit->level);
index++;
}
}
cout << index << ". 返回" << endl;
cout << "选择兵种: ";
int choice, count;
cin >> choice;
if (choice > 0 && choice < index) {
auto selected = unit_map[choice];
cout << "招募数量: ";
cin >> count;
nation->recruitMilitary(selected.first, selected.second, count);
}
}
void upgradeMilitaryMenu(shared_ptr<Nation> nation) {
cout << "\n════════════ 军队升级 ════════════" << endl;
nation->getMilitary().displayForces();
// 简化的升级界面
pr("军队升级功能开发中...\n", 13);
}
void declareWar(shared_ptr<Nation> attacker) {
cout << "\n════════════ 宣战目标 ════════════" << endl;
int index = 1;
vector<int> valid_targets;
for (int i = 0; i < nations.size(); i++) {
if (nations[i]->getName() != attacker->getName() && !nations[i]->isDefeated()) {
cout << index << ". " << nations[i]->getName();
Relation rel = attacker->getRelation(nations[i]->getName());
switch(rel) {
case Relation::ALLY: cout << " (同盟)"; break;
case Relation::NEUTRAL: cout << " (中立)"; break;
case Relation::ENEMY: cout << " (敌国)"; break;
case Relation::VASSAL: cout << " (附庸)"; break;
case Relation::TRADE_PARTNER: cout << " (贸易伙伴)"; break;
}
cout << " - 战力: " << static_cast<int>(nations[i]->getMilitary().totalPower());
cout << " - 时代: " << nations[i]->getEraName(nations[i]->getTechEra()) << endl;
valid_targets.push_back(i);
index++;
}
}
cout << index << ". 返回" << endl;
cout << "选择目标: ";
int choice;
cin >> choice;
if (choice > 0 && choice <= valid_targets.size()) {
int target_index = valid_targets[choice - 1];
BattleSystem::conductBattle(*attacker, *nations[target_index]);
// 更新外交关系
attacker->setRelation(nations[target_index]->getName(), Relation::ENEMY);
nations[target_index]->setRelation(attacker->getName(), Relation::ENEMY);
}
}
void aiTurn(shared_ptr<Nation> nation) {
pr("AI正在思考...\n", 13);
this_thread::sleep_for(chrono::seconds(1)); // 模拟AI思考
AIStrategy strategy(nation);
strategy.executeTurn();
pr("AI回合结束\n", 13);
}
bool checkGameEnd() {
// 检查是否有玩家失败
int alive_nations = 0;
for (const auto& nation : nations) {
if (!nation->isDefeated()) {
alive_nations++;
}
}
if (alive_nations <= 1) {
gameRunning = false;
return true;
}
// 玩家失败
if (nations[0]->isDefeated()) {
gameRunning = false;
return true;
}
return false;
}
void displayGameResult() {
pr("\n════════════ 游戏结束 ════════════\n", 14);
// 找到胜利者
shared_ptr<Nation> winner = nullptr;
double max_score = 0;
for (const auto& nation : nations) {
if (!nation->isDefeated()) {
double score = nation->getEconomy() + nation->getMilitary().totalPower() / 10000 +
nation->getGlobalInfluence() + static_cast<int>(nation->getTechEra()) * 1000;
if (score > max_score) {
max_score = score;
winner = nation;
}
}
}
if (winner) {
pr(("胜利者: " + winner->getName() + " - " + winner->getLeaderName() + "\n").c_str(), 10);
pr(("最终时代: " + winner->getEraName(winner->getTechEra()) + "\n").c_str(), 13);
pr(("游戏回合: " + to_string(totalTurns) + "\n").c_str(), 11);
pr("恭喜获得最终胜利!\n", 14);
} else {
pr("所有国家都已灭亡,没有胜利者\n", 12);
}
// 显示最终排名
displayFinalRankings();
}
void displayFinalRankings() {
pr("\n最终排名:\n", 11);
vector<tuple<double, string, TechEra, int, double>> rankings;
for (const auto& nation : nations) {
double score = nation->getEconomy() + nation->getMilitary().totalPower() / 10000 +
nation->getGlobalInfluence() + static_cast<int>(nation->getTechEra()) * 1000;
rankings.emplace_back(score, nation->getName(), nation->getTechEra(),
nation->getPopulation(), nation->getMilitary().totalPower());
}
sort(rankings.rbegin(), rankings.rend());
for (size_t i = 0; i < rankings.size(); ++i) {
double score;
string name;
TechEra era;
int pop;
double military;
tie(score, name, era, pop, military) = rankings[i];
cout << i + 1 << ". " << name
<< " - 分数: " << static_cast<int>(score)
<< " - 时代: " << getEraName(era)
<< " - 人口: " << (pop > 1000000 ? to_string(pop/1000000) + "M" : to_string(pop/1000) + "K")
<< " - 军力: " << (military > 1000000 ? to_string(static_cast<int>(military)/1000000) + "M" :
military > 1000 ? to_string(static_cast<int>(military)/1000) + "K" : to_string(static_cast<int>(military)))
<< endl;
}
}
string getEraName(TechEra era) {
switch(era) {
case TechEra::STONE_AGE: return "石器时代";
case TechEra::BRONZE_AGE: return "青铜时代";
case TechEra::IRON_AGE: return "铁器时代";
case TechEra::CLASSICAL: return "古典时代";
case TechEra::MEDIEVAL: return "中世纪";
case TechEra::RENAISSANCE: return "文艺复兴";
case TechEra::INDUSTRIAL: return "工业时代";
case TechEra::MODERN: return "现代";
case TechEra::INFORMATION: return "信息时代";
case TechEra::DIGITAL: return "数字时代";
case TechEra::BIOTECH: return "生物科技";
case TechEra::SPACE_AGE: return "太空时代";
default: return "未知";
}
}
// 保存单个国家数据
void saveNation(ofstream& file, const shared_ptr<Nation>& nation) {
// 保存基础信息
string name = nation->getName();
string leader = nation->getLeaderName();
int nameLen = name.length();
int leaderLen = leader.length();
file.write(reinterpret_cast<const char*>(&nameLen), sizeof(nameLen));
file.write(name.c_str(), nameLen);
file.write(reinterpret_cast<const char*>(&leaderLen), sizeof(leaderLen));
file.write(leader.c_str(), leaderLen);
// 保存地形
Terrain terrain = nation->getTerrain();
file.write(reinterpret_cast<const char*>(&terrain), sizeof(terrain));
// 保存资源
Resources res = nation->getResources();
file.write(reinterpret_cast<const char*>(&res), sizeof(res));
// 保存科技
Technology tech = nation->getTechnology();
file.write(reinterpret_cast<const char*>(&tech), sizeof(tech));
// 保存建筑
Buildings buildings = nation->getBuildings();
file.write(reinterpret_cast<const char*>(&buildings), sizeof(buildings));
// 保存国家属性
double economy = nation->getEconomy();
double agriculture = nation->getAgriculture();
double health = nation->getHealth();
double stability = nation->getStability();
double happiness = nation->getHappiness();
double education = nation->getEducation();
int population = nation->getPopulation();
int territory_size = nation->getTerritorySize();
double inflation = nation->getInflation();
int turn_count = nation->getTurnCount();
double global_influence = nation->getGlobalInfluence();
file.write(reinterpret_cast<const char*>(&economy), sizeof(economy));
file.write(reinterpret_cast<const char*>(&agriculture), sizeof(agriculture));
file.write(reinterpret_cast<const char*>(&health), sizeof(health));
file.write(reinterpret_cast<const char*>(&stability), sizeof(stability));
file.write(reinterpret_cast<const char*>(&happiness), sizeof(happiness));
file.write(reinterpret_cast<const char*>(&education), sizeof(education));
file.write(reinterpret_cast<const char*>(&population), sizeof(population));
file.write(reinterpret_cast<const char*>(&territory_size), sizeof(territory_size));
file.write(reinterpret_cast<const char*>(&inflation), sizeof(inflation));
file.write(reinterpret_cast<const char*>(&turn_count), sizeof(turn_count));
file.write(reinterpret_cast<const char*>(&global_influence), sizeof(global_influence));
// 保存军事单位
auto military_units = nation->getMilitaryUnits();
int unit_count = military_units.size();
file.write(reinterpret_cast<const char*>(&unit_count), sizeof(unit_count));
for (const auto& unit : military_units) {
int type_len = unit.type.length();
file.write(reinterpret_cast<const char*>(&type_len), sizeof(type_len));
file.write(unit.type.c_str(), type_len);
file.write(reinterpret_cast<const char*>(&unit.level), sizeof(unit.level));
file.write(reinterpret_cast<const char*>(&unit.count), sizeof(unit.count));
}
// 保存外交关系
auto relations = nation->getAllRelations();
int relationCount = relations.size();
file.write(reinterpret_cast<const char*>(&relationCount), sizeof(relationCount));
for (const auto& rel : relations) {
int nationNameLen = rel.first.length();
file.write(reinterpret_cast<const char*>(&nationNameLen), sizeof(nationNameLen));
file.write(rel.first.c_str(), nationNameLen);
file.write(reinterpret_cast<const char*>(&rel.second), sizeof(rel.second));
}
}
// 加载单个国家数据
shared_ptr<Nation> loadNation(ifstream& file) {
// 读取基础信息
int nameLen, leaderLen;
file.read(reinterpret_cast<char*>(&nameLen), sizeof(nameLen));
string name(nameLen, ' ');
file.read(&name[0], nameLen);
file.read(reinterpret_cast<char*>(&leaderLen), sizeof(leaderLen));
string leader(leaderLen, ' ');
file.read(&leader[0], leaderLen);
// 读取地形
Terrain terrain;
file.read(reinterpret_cast<char*>(&terrain), sizeof(terrain));
auto nation = make_shared<Nation>(name, leader, terrain);
// 读取资源
Resources res;
file.read(reinterpret_cast<char*>(&res), sizeof(res));
nation->getResources() = res;
// 读取科技
Technology tech;
file.read(reinterpret_cast<char*>(&tech), sizeof(tech));
nation->getTechnology() = tech;
// 读取建筑
Buildings buildings;
file.read(reinterpret_cast<char*>(&buildings), sizeof(buildings));
nation->getBuildings() = buildings;
// 读取国家属性
double economy, agriculture, health, stability, happiness, education, inflation, global_influence;
int population, territory_size, turn_count;
file.read(reinterpret_cast<char*>(&economy), sizeof(economy));
file.read(reinterpret_cast<char*>(&agriculture), sizeof(agriculture));
file.read(reinterpret_cast<char*>(&health), sizeof(health));
file.read(reinterpret_cast<char*>(&stability), sizeof(stability));
file.read(reinterpret_cast<char*>(&happiness), sizeof(happiness));
file.read(reinterpret_cast<char*>(&education), sizeof(education));
file.read(reinterpret_cast<char*>(&population), sizeof(population));
file.read(reinterpret_cast<char*>(&territory_size), sizeof(territory_size));
file.read(reinterpret_cast<char*>(&inflation), sizeof(inflation));
file.read(reinterpret_cast<char*>(&turn_count), sizeof(turn_count));
file.read(reinterpret_cast<char*>(&global_influence), sizeof(global_influence));
// 设置国家属性
nation->setAgriculture(agriculture);
nation->setHealth(health);
nation->setStability(stability);
nation->setHappiness(happiness);
nation->setEducation(education);
nation->setPopulation(population);
nation->setTerritorySize(territory_size);
nation->setInflation(inflation);
nation->setTurnCount(turn_count);
nation->setGlobalInfluence(global_influence);
// 读取军事单位
int unit_count;
file.read(reinterpret_cast<char*>(&unit_count), sizeof(unit_count));
for (int i = 0; i < unit_count; i++) {
int type_len;
file.read(reinterpret_cast<char*>(&type_len), sizeof(type_len));
string type(type_len, ' ');
file.read(&type[0], type_len);
int level, count;
file.read(reinterpret_cast<char*>(&level), sizeof(level));
file.read(reinterpret_cast<char*>(&count), sizeof(count));
nation->setMilitaryUnitCount(type, level, count);
}
// 读取外交关系
int relationCount;
file.read(reinterpret_cast<char*>(&relationCount), sizeof(relationCount));
for (int i = 0; i < relationCount; i++) {
int nationNameLen;
file.read(reinterpret_cast<char*>(&nationNameLen), sizeof(nationNameLen));
string otherNation(nationNameLen, ' ');
file.read(&otherNation[0], nationNameLen);
Relation rel;
file.read(reinterpret_cast<char*>(&rel), sizeof(rel));
nation->setRelation(otherNation, rel);
}
return nation;
}
};
int main()
{
Game game;
game.run();
return 0;
}
祝大家玩的开心!
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐



所有评论(0)