Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,12 @@ target_link_libraries(theme_combine_test PRIVATE oc4_options oc4_render)
add_test(NAME theme_combine COMMAND theme_combine_test)
set_target_properties(theme_combine_test PROPERTIES FOLDER "tests")

add_executable(flag_art_test tests/flag_art_test.cpp)
target_include_directories(flag_art_test PRIVATE src)
target_link_libraries(flag_art_test PRIVATE oc4_options oc4_render)
add_test(NAME flag_art COMMAND flag_art_test)
set_target_properties(flag_art_test PROPERTIES FOLDER "tests")

if(WIN32 AND TARGET freetype)
add_executable(text_layout_freetype_test tests/text_layout_test.cpp src/render/TextLayout.cpp src/render/FontFreeType.cpp)
target_include_directories(text_layout_freetype_test PRIVATE src)
Expand Down
85 changes: 83 additions & 2 deletions apps/oc4core/MapCapture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ namespace oc4::host
}
loadCityLSystem();
loadPlotLSystem();
loadFlagScale();
loadAttachables(s);

s.rivers.clear();
Expand All @@ -250,6 +251,61 @@ namespace oc4::host
m_haveArt = true;
}

// Flags have their own zoom-dependent scale, separate from UNIT_SCALE; the health bars their
// size and place. Mods may replace them.
void MapCapture::loadFlagScale()
{
const auto loc = m_assets.locate("xml/misc/civ4detailmanager.xml");
if (!loc)
return;
std::string error;
const auto doc = parseXml(m_assets.read(*loc), error);
if (!doc)
return;
const std::pair<const wchar_t*, std::vector<std::pair<float, float>>*> wanted[] = { { L"FLAG_SCALE", &m_snapshot.flagScale },
{ L"UNIT_HEALTH_BILLBOARD_SCALE", &m_snapshot.healthScale }, { L"UNIT_HEALTH_BILLBOARD_Z", &m_snapshot.healthZ },
{ L"UNIT_HEALTH_BILLBOARD_RADIUS", &m_snapshot.healthRadius } };
for (const auto& node : doc->children)
if (node->type == XmlNode::Type::Element)
for (const auto& fader : node->children)
{
const XmlNode* name = fader->type == XmlNode::Type::Element && fader->name == "Fader" ? fader->firstChildElement("Name") : nullptr;
const auto target = name ? std::ranges::find(wanted, name->text, [](const auto& w) { return std::wstring_view(w.first); }) : std::end(wanted);
if (target == std::end(wanted))
continue;
const bool scale = target->second == &m_snapshot.flagScale || target->second == &m_snapshot.healthScale;
std::vector<std::pair<float, float>> keys;
for (const auto& key : fader->children)
if (key->type == XmlNode::Type::Element && key->name == "Key")
{
float distance = 0, value = 0;
if (std::swscanf(key->text.c_str(), L"%f , %f", &distance, &value) == 2 && distance >= 0 && (value > 0 || !scale))
keys.emplace_back(distance, value);
}
if (!keys.empty())
{
std::ranges::sort(keys);
*target->second = std::move(keys);
}
}
}

FlagArt flagArt(int player)
{
FlagArt f;
if (player < 0 || player >= GC.getMAX_PLAYERS())
return f;
const CvPlayerAI& pl = CvPlayerAI::getPlayerNonInl(static_cast<PlayerTypes>(player));
f.primary = colorOf(pl.getPlayerColor(), true);
f.secondary = colorOf(pl.getPlayerColor(), false);
f.decal = utf8(pl.getFlagDecal().c_str());
f.white = pl.isWhiteFlag();
const int era = pl.getCurrentEra(), medieval = GC.getInfoTypeForString("ERA_MEDIEVAL", true), industrial = GC.getInfoTypeForString("ERA_INDUSTRIAL", true);
f.model = ARTFILEMGR.getMiscArtInfo(industrial >= 0 && era >= industrial ? "ART_UNITGROUP_FLAG_MODERN" :
medieval >= 0 && era >= medieval ? "ART_UNITGROUP_FLAG_MEDIEVAL" : "ART_UNITGROUP_FLAG_ANCIENT");
return f;
}

int MapCapture::modelIndex(const void* art, const char* nif, const char* kfm, float scale, const char* shadow, const char* shadowNode, float shadowScale)
{
if (const auto it = m_modelByArt.find(art); it != m_modelByArt.end())
Expand Down Expand Up @@ -768,6 +824,15 @@ namespace oc4::host
s.cameraStartDistance = defineOr("CAMERA_START_DISTANCE", 2200);
s.cameraTopDistance = defineOr("CAMERA_SMALLEST_MAX_DISTANCE", 7000);
s.cameraCityDistance = defineOr("CAMERA_CITY_ZOOM_IN_DISTANCE", 3500);
s.flagOffset = { GC.getDefineFLOAT("FLAG_OFFSET_X"), GC.getDefineFLOAT("FLAG_OFFSET_Y") };
const auto miscModel = [&](const char* tag) {
const CvArtInfoMisc* art = ARTFILEMGR.getMiscArtInfo(tag);
return art ? modelIndex(art, art->getNIF(), art->getKFM(), art->getScale()) : -1;
};
s.flagGlow = miscModel("ART_UNITGROUP_FLAG_GLOW");
// (no art define names the flag's medallions: the engine's own files, as the original's)
static const char medallionsArt = 0;
s.flagMedallions = modelIndex(&medallionsArt, "Art/Units/Flag/Medallions.nif", "Art/Units/Flag/Medallions.kfm", 1);
// (the pitches are negative there: below the horizon)
s.cameraLowerPitch = std::abs(GC.getDefineFLOAT("CAMERA_LOWER_PITCH")) > 0 ? std::abs(GC.getDefineFLOAT("CAMERA_LOWER_PITCH")) : 28;
s.cameraUpperPitch = std::abs(GC.getDefineFLOAT("CAMERA_UPPER_PITCH")) > 0 ? std::abs(GC.getDefineFLOAT("CAMERA_UPPER_PITCH")) : 90;
Expand All @@ -791,8 +856,12 @@ namespace oc4::host
v.alive = pl.isAlive();
if (!v.alive)
continue;
v.primary = colorOf(pl.getPlayerColor(), true);
v.secondary = colorOf(pl.getPlayerColor(), false);
const FlagArt flag = flagArt(p);
v.primary = flag.primary;
v.secondary = flag.secondary;
v.flagDecal = flag.decal;
v.whiteFlag = flag.white;
v.flagModel = flag.model ? modelIndex(flag.model, flag.model->getNIF(), flag.model->getKFM(), flag.model->getScale()) : -1;
v.name = utf8(pl.getName());
v.civilization = utf8(pl.getCivilizationShortDescription());
v.score = game.getPlayerScore(static_cast<PlayerTypes>(p));
Expand Down Expand Up @@ -940,7 +1009,19 @@ namespace oc4::host
u.actionEffect = a.effect;
}
u.count = seenCount;
for (int k = 0; k < count && u.medallions.size() < 7; ++k)
if (const CvUnit* any = plot->getUnitByIndex(k); seen(any))
u.medallions.push_back(!selection || !selection->isSelected(any) || any->getOwner() != GC.getGame().getActivePlayer() ? 3
: !any->canMove() ? 2 : any->hasMoved() ? 4 : 1);
u.health = shown->maxHitPoints() > 0 ? float(shown->currHitPoints()) / float(shown->maxHitPoints()) : 1.0f;
// (the health bar's length, as the engine asks the DLL for it: the unit's strength
// now times CvUnit::getHealthBarModifier, HEALTH_BAR_WIDTH over twice the best land
// unit's strength; none for a unit without strength, nor before there is a best
// land unit: the modifier is then not finite. Only DllExport functions: the
// Windows DLL exports nothing else.)
if (const float modifier = shown->getHealthBarModifier(); std::isfinite(modifier))
u.healthBar = (shown->getDomainType() == DOMAIN_AIR ? shown->airCurrCombatStrFloat(nullptr) : shown->currCombatStrFloat(nullptr, nullptr)) *
modifier;
u.promotionReady = shown->isPromotionReady();
if (const char* button = shown->getButton())
u.button = button;
Expand Down
13 changes: 13 additions & 0 deletions apps/oc4core/MapCapture.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@

namespace oc4::host
{
// A player's flag as the engine draws it, on the map and by the minimap: the flag model of
// the player's era (ART_UNITGROUP_FLAG_ANCIENT, _MEDIEVAL from the medieval era, _MODERN
// from the industrial), the civilization's decal, the player's colours.
struct FlagArt
{
const class CvArtInfoMisc* model = nullptr;
std::string decal;
render::Color primary, secondary;
bool white = false; // a full-colour decal, its alpha no colour mask
};
FlagArt flagArt(int player);

class MapCapture
{
public:
Expand Down Expand Up @@ -48,6 +60,7 @@ namespace oc4::host
void loadCityLSystem();
void loadPlotLSystem();
int plotLayout(const class CvPlot& plot, int bonus);
void loadFlagScale();
void loadAttachables(render::MapSnapshot& s);
public:
// The cities with the Great Wall (their plots, the map's): the wall along their owners'
Expand Down
16 changes: 16 additions & 0 deletions apps/oc4core/Screens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "GameFont.h"
#include "Interface.h"
#include "MapCapture.h"
#include "Utility.h"
#include "Sounds.h"

Expand Down Expand Up @@ -1236,6 +1237,21 @@ if _walls:
std::sscanf(item.text.c_str(), "%d %d %d", &item.attitude, &item.action, &item.actionSerial);
item.text.clear();
}
if (item.kind == Kind::Flag)
{
// (the player's: the map's flag model of its era, its colours)
const FlagArt flag = flagArt(std::atoi(item.text.c_str()));
if (flag.model && flag.model->getNIF())
{
item.flagNif = flag.model->getNIF();
item.flagKfm = flag.model->getKFM() ? flag.model->getKFM() : "";
item.texture = (flag.white ? "opaque:" : "") + flag.decal;
item.hasColor = true;
item.color = flag.primary;
item.flagSecondary = flag.secondary;
}
item.text.clear();
}
if (item.kind == Kind::Model)
{
// ("tilt turn scale animated|button", "...|button|layers")
Expand Down
9 changes: 6 additions & 3 deletions apps/oc4core/python/oc4screens.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,12 +817,15 @@ def addBonusGraphicGFC(self, szName, iBonus, iX, iY, iWidth, iHeight, eWidgetTyp
pass
self._model(szName, art, info.getButton(), iX, iY, iWidth, iHeight, eWidgetType, iData1, iData2, fXRot, fZRot, fScale, bAnimate)
def addFlagWidgetGFC(self, szName, iX, iY, iWidth, iHeight, iOwner, eWidgetType=-1, iData1=-1, iData2=-1):
"""A player's flag (the main interface's by the minimap): its civilization's flag picture,
an emblem on a banner in the player's colour; a white flag's picture is the whole flag in
its own colours (its alpha is no mask: "opaque:")."""
"""A player's flag (the main interface's by the minimap): the host draws the flag model of
the player's era waving, in the player's colours with the civilization's emblem (text:
the player). Where it cannot, its civilization's flag picture, an emblem on a banner in
the player's colour; a white flag's picture is the whole flag in its own colours (its
alpha is no mask: "opaque:")."""
w = self._s.add(szName, 'flag')
w.x, w.y, w.w, w.h = int(iX), int(iY), int(iWidth), int(iHeight)
w.wt, w.d1, w.d2 = _int(eWidgetType), _int(iData1), _int(iData2)
w.text = u'%d' % _int(iOwner)
try:
gc = _cpe.CyGlobalContext()
player = gc.getPlayer(int(iOwner))
Expand Down
35 changes: 35 additions & 0 deletions src/render/ButtonArt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

#include "core/FileUtil.h"
#include "gfx/DdsImage.h"
#include "render/MapSnapshot.h"

#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <filesystem>

namespace oc4::render
{
namespace
{
constexpr uint64_t kIconKeys = 1ull << 40; // overlay picture keys (0 is the minimap)
constexpr uint64_t kFlagKeys = 2ull << 40; // the map's flag art has its own ButtonArt cache
constexpr uint32_t kAtlasCell = 64;
}

Expand Down Expand Up @@ -50,6 +53,38 @@ namespace oc4::render
return p.width ? &p : nullptr;
}

const ButtonArt::Icon* ButtonArt::flag(const std::string& art, const Color& primary, const Color& secondary, bool whiteFlag)
{
const auto pack = [](const Color& c) {
const auto channel = [](float v) { return uint32_t(std::lround(std::clamp(v, 0.0f, 1.0f) * 255)); };
return channel(c.b) | channel(c.g) << 8 | channel(c.r) << 16;
};
const uint32_t p = pack(primary), s = pack(secondary);
const auto key = std::tuple{ makeAssetKey(art), p, s, whiteFlag };
if (const auto it = m_flags.find(key); it != m_flags.end())
return it->second.width ? &it->second : nullptr;
Icon& icon = m_flags[key];
const Picture* source = picture(art);
if (!source)
return nullptr;
icon.width = source->width;
icon.height = source->height;
icon.bgra = source->bgra;
for (size_t i = 0; i + 3 < icon.bgra.size(); i += 4)
{
const unsigned a = icon.bgra[i + 3];
if (!whiteFlag)
for (unsigned c = 0; c < 3; ++c)
{
const unsigned colour = (((p >> (c * 8)) & 255) * (255 - a) + ((s >> (c * 8)) & 255) * a + 127) / 255;
icon.bgra[i + c] = uint8_t((unsigned(icon.bgra[i + c]) * colour + 127) / 255);
}
icon.bgra[i + 3] = 255; // the decal's alpha is a colour mask, not cloth transparency
}
icon.key = kFlagKeys + ++m_next;
return &icon;
}

void ButtonArt::setFontLayout(std::vector<FontGroup> groups)
{
m_fontLayout = std::move(groups);
Expand Down
6 changes: 6 additions & 0 deletions src/render/ButtonArt.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
#include <cstdint>
#include <map>
#include <string>
#include <tuple>
#include <vector>

namespace oc4::render
{
struct Color;

class ButtonArt
{
public:
Expand All @@ -31,6 +34,8 @@ namespace oc4::render

// nullptr when there is none.
const Icon* icon(const std::string& art);
// A flag decal's alpha blends the player's two colours; white flags use their own RGB.
const Icon* flag(const std::string& art, const Color& primary, const Color& secondary, bool whiteFlag);
// The DLL's groups of the font's icons (CvGameTextMgr::getFontSymbols): their
// characters and how many rows of 25 cells each group takes in the font.
struct FontGroup
Expand Down Expand Up @@ -58,6 +63,7 @@ namespace oc4::render

const AssetResolver& m_assets;
std::map<std::string, Icon> m_icons;
std::map<std::tuple<std::string, uint32_t, uint32_t, bool>, Icon> m_flags;
std::map<std::string, Picture> m_pictures; // decoded DDS files (the atlases)
uint64_t m_next = 0;
};
Expand Down
19 changes: 19 additions & 0 deletions src/render/MapSnapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ namespace oc4::render
struct PlayerView
{
Color primary, secondary;
std::string flagDecal;
bool whiteFlag = false; // a full-colour flag instead of a two-colour alpha mask
int flagModel = -1; // era's flag model and animation, into MapSnapshot::models
bool alive = false;
std::string name, civilization;
int score = 0, cities = 0;
Expand Down Expand Up @@ -158,6 +161,7 @@ namespace oc4::render
std::vector<int> models; // one per figure of the formation, into MapSnapshot::models
int count = 1; // units on the plot
float health = 1;
float healthBar = 0; // its health bar's length in the original's units (its strength now)
std::string button; // the shown unit's button art (the unit layer's icons)
bool selected = false; // the player has selected units there
bool promotionReady = false; // enough experience and at least one promotion it can take
Expand All @@ -183,6 +187,9 @@ namespace oc4::render
int actionX = -1, actionY = -1;
std::string actionEffect;
bool air = false; // a plane: its missions are flown to their plot and back
// The flag's medallions: one a unit seen on the plot (up to 7), its state as
// Medallions.kfm's (1 can move, 2 cannot, 3 white: not selected, 4 moved, can go on).
std::vector<uint8_t> medallions;
};

// A battle the game shows (its CvBattleDefinition): the game holds it for `slices` of its
Expand Down Expand Up @@ -228,6 +235,18 @@ namespace oc4::render
{
// Content (art); `artGeneration` changes when it does.
uint64_t artGeneration = 0;
int flagGlow = -1; // selected unit's flag highlight, into models
// The medallions over a flag (Art/Units/Flag/Medallions.nif): one a unit on the plot, up to
// 7, white, or for the selected units how they can move; into models.
int flagMedallions = -1;
struct { float x = -0.667f, y = 0; } flagOffset; // GlobalDefines, relative to half a plot
// CIV4DetailManager's FLAG_SCALE: camera distance, model scale multiplier.
std::vector<std::pair<float, float>> flagScale{ { 700, 0.6f }, { 1500, 0.7f }, { 3000, 0.9f }, { 5000, 1.2f } };
// A unit's health bar (CIV4DetailManager's UNIT_HEALTH_BILLBOARD_* by camera distance): its
// scale (how thick it is), its height over the ground, and how far from the plot's middle
// towards the camera it stands (less: behind it).
std::vector<std::pair<float, float>> healthScale{ { 700, 3 }, { 2500, 2 } }, healthZ{ { 700, 80 }, { 5000, 70 } },
healthRadius{ { 700, 0 }, { 2000, -60 }, { 5000, -80 } };
std::vector<TerrainArt> terrains; // by TerrainTypes
int hillTerrain = -1, peakTerrain = -1; // graphical terrains drawn over hills and peaks
std::vector<FeatureArt> features; // by FeatureTypes
Expand Down
Loading
Loading