diff --git a/apps/oc4core/MapCapture.cpp b/apps/oc4core/MapCapture.cpp index e61f159..83309a5 100644 --- a/apps/oc4core/MapCapture.cpp +++ b/apps/oc4core/MapCapture.cpp @@ -421,6 +421,30 @@ namespace oc4::host stack.pop_back(); for (const auto& c : n->children) stack.push_back(c.get()); + // (the paving of the 2x2 lots: Node_2x2's productions' decorations, by era) + if (n->type == XmlNode::Type::Element && n->name == "LProduction") + { + const std::wstring* from = n->attribute("From"); + if (!from || *from != L"Node_2x2") + continue; + for (const auto& ref : n->children) + { + const std::wstring* name = ref->type == XmlNode::Type::Element && ref->name == "ArtRef" ? ref->attribute("Name") : nullptr; + if (!name || name->rfind(L"decoration:", 0) != 0) + continue; + const std::string value = narrow(name->substr(11)); + const size_t sep = value.find("::"); + LSystemHouse paving; + paving.art = int(m_snapshot.cityHouses.size()); + m_snapshot.cityHouses.push_back({ "Art/Structures/Cities/" + value.substr(0, sep), sep == std::string::npos ? "" : value.substr(sep + 2), 2, 2 }); + for (const auto& a : ref->children) + if (const std::wstring* cls = a->type == XmlNode::Type::Element && a->name == "Attribute" ? a->attribute("Class") : nullptr) + if (*cls == L"Era") + paving.eras = list(a->text); + m_lotPavings.push_back(std::move(paving)); + } + continue; + } if (n->type != XmlNode::Type::Element || n->name != "LNode") continue; int width = 1, height = 1; @@ -1131,6 +1155,16 @@ namespace oc4::host std::copy(yields, yields + 3, shown.yields.begin()); } + // The paving under a city's lots in its era (none: -1). + int MapCapture::lotPaving(int era) const + { + const std::string eraName = era >= 0 && era < GC.getNumEraInfos() ? text(GC.getEraInfo(static_cast(era)).getType()) : ""; + for (const LSystemHouse& p : m_lotPavings) + if (p.eras.empty() || std::ranges::find(p.eras, eraName) != p.eras.end() || std::ranges::find(p.eras, std::string("ERA_ALL")) != p.eras.end()) + return p.art; + return -1; + } + const render::MapSnapshot& MapCapture::capture(const Interface* selection) { if (!m_haveArt) @@ -1521,6 +1555,17 @@ namespace oc4::host } } c.houseSet = c.owner >= 0 ? houseSet(player(c.owner).getCurrentEra(), city->getArtStyleType()) : -1; + c.paving = c.owner >= 0 ? lotPaving(player(c.owner).getCurrentEra()) : -1; + } + // The effects over it, as the original engine asks for them: at the detail zoom + // while the city screen is up. + { + std::vector names; + city->getVisibleEffects(selection && const_cast(selection)->isCityScreenUp() ? ZOOM_DETAIL : ZOOM_NORMAL, names); + c.effects.clear(); + for (const TCHAR* n : names) + if (n) + c.effects.emplace_back(n); } s.cities.push_back(c); } diff --git a/apps/oc4core/MapCapture.h b/apps/oc4core/MapCapture.h index 245309f..da78c7e 100644 --- a/apps/oc4core/MapCapture.h +++ b/apps/oc4core/MapCapture.h @@ -87,6 +87,7 @@ namespace oc4::host std::vector> m_greatWallCities; void loadArtList(std::vector& out, const char* file, const char* element); int houseSet(int era, int artStyle); + int lotPaving(int era) const; // A generic house of the city LSystem with the eras and art styles it is drawn for. struct LSystemHouse @@ -95,6 +96,10 @@ namespace oc4::host std::vector eras, styles; }; + // The paving the LSystem lays under its 2x2 lots (Node_2x2's "decoration:" art) with the + // eras it is for. + std::vector m_lotPavings; + // The LSystem's walls (szBuilder:WallBuilder) by LSystem name: post and wall files // (empty: the generic walls of the city's era). std::map> m_wallBuilders; diff --git a/src/render/MapSnapshot.h b/src/render/MapSnapshot.h index e95456d..87ac19f 100644 --- a/src/render/MapSnapshot.h +++ b/src/render/MapSnapshot.h @@ -106,7 +106,7 @@ namespace oc4::render // holds a set of them ("1x1_01"), covering width x height lots of the city's layout. struct CityHouseArt { - std::string nif, node; + std::string nif, node; // (no node: the whole model) int width = 1, height = 1; }; @@ -255,6 +255,10 @@ namespace oc4::render std::vector walls; int generics = 0; int houseSet = -1; // into MapSnapshot::houseSets + int paving = -1; // into MapSnapshot::cityHouses: the paving of its lots in pairs (2x2), for its era + // The effects the game core shows over it (CvCity::getVisibleEffects: an occupied city's + // fire, an angry one's smoke, an unhealthy one's fumes, fireworks): CIV4EffectInfos types. + std::vector effects; }; struct MapSnapshot @@ -316,6 +320,7 @@ namespace oc4::render // What the plot LSystem lays out on plots (PlotView::layout): farms. std::vector> plotLayouts; float cityLayoutSize = 0.72f, cityTwist = -0.7853982f; + float cityTreeCut = 1.0f; // CITY_TREECUT_SCALE: the trees in a circle so many plots across round a city's middle go // The map. World units as the game's (PLOT_SIZE per plot); heights of hills and peaks // in plots (CvLandscapeInfo). diff --git a/src/render/MapView.cpp b/src/render/MapView.cpp index bae1b95..1d45158 100644 --- a/src/render/MapView.cpp +++ b/src/render/MapView.cpp @@ -2753,6 +2753,13 @@ float4 PSWater(WaterOut i) : SV_Target return px + py * map.width; }; std::unordered_map> waters; // by plot + // (a city's: a circle CITY_TREECUT_SCALE plots across round its middle, as measured + // in the original: the trees come half a plot from a size 16 city's middle) + for (const CityView& c : map.cities) + { + const Vec3 middle{ (float(c.x) + 0.5f) * S(), (float(c.y) + 0.5f) * S(), 0 }; + cuts[plotKey(middle.x, middle.y)].push_back({ middle, map.cityTreeCut * S() / 2 }); + } for (const Instance& d : decor) { for (const auto& [c, r] : d.model->treeCuts) @@ -2979,24 +2986,28 @@ float4 PSWater(WaterOut i) : SV_Target cities.clear(); addGreatWall(); // The layout's size is the detail manager's CITY_LAYOUT_SIZE; taken as half the side - // of the turned square here, which gives the original's proportions (a large city - // reaches into the plots around it). A 1x1 house of the generic sets is some 55 - // units across, about a lot. + // of the turned square here, which gives the original's proportions (measured: a + // size 16 city's paving is 0.39 of its 5-plot border across). The LSystem's art is + // 33.4 units a lot (its houses, and the paving: mod_city_lot.nif's 4x4 is 133.6 + // across), scaled to the lot so that houses and paving stand side by side. constexpr int kLots = 16; + constexpr float kLotArt = 33.4f; const float lot = 2 * map.cityLayoutSize * S() / kLots; - const float scale = lot / 50.0f; + const float scale = lot / kLotArt; const float c0 = std::cos(map.cityTwist), s0 = std::sin(map.cityTwist); std::unordered_map layouts; for (const CityView& c : map.cities) { std::string key; const auto put = [&key](const auto& v) { key.append(reinterpret_cast(&v), sizeof(v)); }; - put(c.owner), put(c.generics), put(c.houseSet), put(teamColor(c.owner)), put(lot), put(map.cityTwist); + put(c.owner), put(c.generics), put(c.houseSet), put(c.paving), put(teamColor(c.owner)), put(lot), put(map.cityTwist); for (const CityBuildingView& b : c.buildings) put(b.model), put(b.width), put(b.height), put(b.water), put(b.scale); for (const CityWallView& w : c.walls) for (const std::string* s : { &w.post, &w.wall, &w.gate }) put(s->size()), key += *s; + for (const std::string& e : c.effects) + put(e.size()), key += e; const int plotIndex = c.x + c.y * map.width; if (const auto kept = cityLayouts.find(plotIndex); kept != cityLayouts.end() && kept->second.key == key) { @@ -3007,13 +3018,14 @@ float4 PSWater(WaterOut i) : SV_Target const size_t first = cities.size(); const Vec3 center{ (float(c.x) + 0.5f) * S(), (float(c.y) + 0.5f) * S(), 0 }; std::array taken{}; - // Lots by their distance from the middle, ties broken by the city's own order. + // Lots by their distance from the middle, ties broken by the city's own order: a + // tight block, square as the LSystem's nodes are. std::array rank; std::array order; for (int i = 0; i < kLots * kLots; ++i) { const float dx = float(i % kLots) - 7.5f, dy = float(i / kLots) - 7.5f; - rank[size_t(i)] = std::max(std::abs(dx), std::abs(dy)) + std::hypot(dx, dy) * 0.3f + unitFloat(hash(c.x, c.y, i)) * 1.5f; + rank[size_t(i)] = std::max(std::abs(dx), std::abs(dy)) + std::hypot(dx, dy) * 0.05f + unitFloat(hash(c.x, c.y, i)) * 0.5f; order[size_t(i)] = i; } std::ranges::sort(order, [&](int a, int b) { return rank[size_t(a)] < rank[size_t(b)]; }); @@ -3088,14 +3100,68 @@ float4 PSWater(WaterOut i) : SV_Target inst.team = teamColor(c.owner); cities.push_back(std::move(inst)); } + // The paving under the lots in use, a piece for each pair of pairs of lots (the + // LSystem's Node_2x2 decoration of the city's era). + if (c.paving >= 0 && size_t(c.paving) < map.cityHouses.size()) + { + const CityHouseArt& art = map.cityHouses[size_t(c.paving)]; + if (LoadedModel* lm = model(art.nif, {})) + { + const LoadedModel::Part* part = nullptr; + if (!art.node.empty()) + { + buildParts(*lm); + const auto it = std::ranges::find_if(lm->parts, [&](const LoadedModel::Part& p) { + return toLowerAscii(lm->model.nodes[size_t(p.node)].name) == toLowerAscii(art.node); + }); + part = it != lm->parts.end() ? &*it : nullptr; + } + if (art.node.empty() || part) + for (int by = 0; by < kLots; by += 2) + for (int bx = 0; bx < kLots; bx += 2) + { + if (!taken[size_t(by * kLots + bx)] && !taken[size_t(by * kLots + bx + 1)] && !taken[size_t((by + 1) * kLots + bx)] && + !taken[size_t((by + 1) * kLots + bx + 1)]) + continue; + const Vec3 at = lotsCenter(bx + by * kLots, 2, 2, false); + const Vec3 middle = part ? part->bounds.center() : lm->bounds.center(); + Instance inst = part ? placeStatic(lm, placement(at, map.cityTwist, scale), translation({ -middle.x, -middle.y, 0 }), &part->visible, &part->bounds) + : placeStatic(lm, placement(at, map.cityTwist, scale), translation({ -middle.x, -middle.y, 0 })); + inst.team = teamColor(c.owner); + cities.push_back(std::move(inst)); + } + } + } for (const CityWallView& w : c.walls) buildWalls(c, w, taken, lot, c0, s0, center); placeShoreBuildings(c, center, scale); + placeCityEffects(c, center, plotIndex); layouts[plotIndex] = { std::move(key), { cities.begin() + std::ptrdiff_t(first), cities.end() } }; } cityLayouts = std::move(layouts); } + // The effects the game core shows over the city (its smoke, fumes, fire): each its + // CIV4EffectInfos model at the middle of the plot, at the effect's scale, kept going + // as long as the city shows it. + void placeCityEffects(const CityView& c, Vec3 center, int plotIndex) + { + for (const std::string& type : c.effects) + { + const auto art = std::ranges::find_if(map.effects, [&](const auto& a) { return _stricmp(a.type.c_str(), type.c_str()) == 0; }); + if (art == map.effects.end()) + continue; + LoadedModel* lm = model(art->nif, {}); + if (!lm) + continue; + Vec3 at = center; + at.z = std::max(ground(at.x, at.y), 0.0f); + Instance inst = placeStatic(lm, placement(at, 0, art->scale > 0 ? art->scale : 1)); + inst.plot = plotIndex; + cities.push_back(std::move(inst)); + } + } + // Harbours, lighthouses and the like stand on the shore, towards the water around the // city, side by side along it. void placeShoreBuildings(const CityView& c, Vec3 center, float scale) @@ -3129,7 +3195,7 @@ float4 PSWater(WaterOut i) : SV_Target } } - // The walls round the lots in use (one lot out), turned with the city: posts at the + // The walls round the lots in use (on the paving's edge), turned with the city: posts at the // corners and between the pieces, each side filled with the longest pieces that fit // (4, 2 and 1 lengths: a side of the whole layout takes one of each), the gate in the // middle of the side facing south. @@ -3148,10 +3214,10 @@ float4 PSWater(WaterOut i) : SV_Target } if (x1 < 0) return; - x0 = std::max(x0 - 1, 0); - y0 = std::max(y0 - 1, 0); - x1 = std::min(x1 + 2, kLots); - y1 = std::min(y1 + 2, kLots); + // (on the paving's edge: the pairs of lots in use) + x0 &= ~1, y0 &= ~1; + x1 = std::min((x1 | 1) + 1, kLots); + y1 = std::min((y1 | 1) + 1, kLots); const auto world = [&](float lx, float ly) { lx = (lx - kLots / 2.0f) * lot; ly = (ly - kLots / 2.0f) * lot; @@ -4785,12 +4851,17 @@ float4 PSWater(WaterOut i) : SV_Target { const PlotView& p = snapshot.plots[i]; shape[i] = uint8_t((p.water ? 1 : 0) | (p.hills ? 2 : 0) | (p.peak ? 4 : 0) | (p.riverSouth ? 8 : 0) | (p.riverEast ? 16 : 0)); - featureKey[i] = int32_t(p.feature) << 8 | p.featureVariety; + // (the feature as unsigned: no feature, -1, would set every high bit, the city's bit 30 among them) + featureKey[i] = int32_t(uint16_t(p.feature)) << 8 | p.featureVariety; // (the route's 8 bits and the layout's 16: two of them 256 or 65536 apart on a plot would look unchanged) decorKey[i] = uint64_t(uint16_t(p.improvement)) | uint64_t(uint16_t(p.bonus)) << 16 | uint64_t(uint8_t(p.route)) << 32 | uint64_t(p.riverSouth) << 40 | uint64_t(p.riverEast) << 41 | uint64_t(uint16_t(p.layout + 1)) << 42 | uint64_t(p.improvementState & 3) << 58 | uint64_t(p.bonusState & 3) << 60; } + // (a city clears the trees round it: a new one cuts them again) + for (const CityView& c : snapshot.cities) + if (const int64_t at = int64_t(c.x) + int64_t(c.y) * snapshot.width; at >= 0 && size_t(at) < featureKey.size()) + featureKey[size_t(at)] |= 1 << 30; const bool reshaped = resized || shape != m->shape; const bool refeatured = reshaped || newArt || featureKey != m->featureKey; const bool redecorated = reshaped || newArt || decorKey != m->decorKey;