stad
This commit is contained in:
@@ -3,8 +3,23 @@
|
||||
|
||||
#include <QString>
|
||||
#include <QVector3D>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include <map>
|
||||
#include <cmath>
|
||||
#include "terrain.h"
|
||||
|
||||
struct City {
|
||||
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;
|
||||
@@ -12,9 +27,70 @@ struct City {
|
||||
int tileY;
|
||||
QVector3D worldPos;
|
||||
|
||||
City() : id(0), playerId(0), name(""), tileX(0), tileY(0), worldPos(0, 0, 0) {}
|
||||
City(int cId, int pId, const QString &cName, int tx, int ty, const QVector3D &pos)
|
||||
: id(cId), playerId(pId), name(cName), tileX(tx), tileY(ty), worldPos(pos) {}
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user