97 lines
2.4 KiB
C++
97 lines
2.4 KiB
C++
#ifndef CITY_H
|
|
#define CITY_H
|
|
|
|
#include <QString>
|
|
#include <QVector3D>
|
|
#include <vector>
|
|
#include <utility>
|
|
#include <map>
|
|
#include <cmath>
|
|
#include "terrain.h"
|
|
|
|
struct CityYieldSummary {
|
|
int food = 0;
|
|
int labor = 0;
|
|
int science = 0;
|
|
int culture = 0;
|
|
int faith = 0;
|
|
int gold = 0;
|
|
};
|
|
|
|
class City {
|
|
public:
|
|
int id;
|
|
int playerId;
|
|
QString name;
|
|
int tileX;
|
|
int tileY;
|
|
QVector3D worldPos;
|
|
|
|
int inhabitants;
|
|
int inhabitantsPerPop;
|
|
|
|
std::vector<std::pair<int, int>> territory; // Owned hex coordinates
|
|
std::map<std::pair<int, int>, QString> workedTiles; // (x, y) -> resource name
|
|
|
|
City(int cId, int pId, const QString &cName, int tx, int ty, const QVector3D &pos,
|
|
int initInhabitants = 1000, int initInhabitantsPerPop = 100000, int initRadius = 1);
|
|
|
|
int getTotalPop() const {
|
|
return (int)std::ceil((float)inhabitants / (float)std::max(1, inhabitantsPerPop));
|
|
}
|
|
|
|
int getAssignedPop() const {
|
|
return (int)workedTiles.size();
|
|
}
|
|
|
|
int getAvailablePop() const {
|
|
return std::max(0, getTotalPop() - getAssignedPop());
|
|
}
|
|
|
|
bool ownsTile(int tx, int ty) const {
|
|
for (const auto &p : territory) {
|
|
if (p.first == tx && p.second == ty) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool isTileWorked(int tx, int ty) const {
|
|
return workedTiles.find({tx, ty}) != workedTiles.end();
|
|
}
|
|
|
|
QString getWorkedResource(int tx, int ty) const {
|
|
auto it = workedTiles.find({tx, ty});
|
|
return it != workedTiles.end() ? it->second : "";
|
|
}
|
|
|
|
bool assignWorker(int tx, int ty, const QString &resource) {
|
|
if (!ownsTile(tx, ty)) return false;
|
|
if (isTileWorked(tx, ty)) {
|
|
workedTiles[{tx, ty}] = resource;
|
|
return true;
|
|
}
|
|
if (getAvailablePop() <= 0) return false;
|
|
workedTiles[{tx, ty}] = resource;
|
|
return true;
|
|
}
|
|
|
|
bool unassignWorker(int tx, int ty) {
|
|
auto it = workedTiles.find({tx, ty});
|
|
if (it != workedTiles.end()) {
|
|
workedTiles.erase(it);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void toggleWorker(int tx, int ty, const QString &defaultRes) {
|
|
if (isTileWorked(tx, ty)) {
|
|
unassignWorker(tx, ty);
|
|
} else {
|
|
assignWorker(tx, ty, defaultRes);
|
|
}
|
|
}
|
|
};
|
|
|
|
#endif // CITY_H
|