Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TeamColor elements #381

Merged
merged 5 commits into from
Jan 7, 2024
Merged
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
10 changes: 9 additions & 1 deletion Avara.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
9419E8472B49ECB1007C50D0 /* CTeamColorAdjuster.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9419E8462B49ECB1007C50D0 /* CTeamColorAdjuster.cpp */; };
9419E8482B49ECB1007C50D0 /* CTeamColorAdjuster.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9419E8462B49ECB1007C50D0 /* CTeamColorAdjuster.cpp */; };
944F2F702B323F1900856E53 /* LocalAssetRepository.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 944F2F6F2B323F1900856E53 /* LocalAssetRepository.cpp */; };
944F2F712B323F1900856E53 /* LocalAssetRepository.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 944F2F6F2B323F1900856E53 /* LocalAssetRepository.cpp */; };
9465BFD22B30C2A00050681C /* AssetManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9465BFD12B30C2A00050681C /* AssetManager.cpp */; };
Expand Down Expand Up @@ -365,6 +367,8 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
9419E8452B49EC89007C50D0 /* CTeamColorAdjuster.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CTeamColorAdjuster.h; sourceTree = "<group>"; };
9419E8462B49ECB1007C50D0 /* CTeamColorAdjuster.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = CTeamColorAdjuster.cpp; sourceTree = "<group>"; };
944F2F6D2B32379C00856E53 /* AssetRepository.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AssetRepository.h; sourceTree = "<group>"; };
944F2F6E2B323E2100856E53 /* LocalAssetRepository.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LocalAssetRepository.h; sourceTree = "<group>"; };
944F2F6F2B323F1900856E53 /* LocalAssetRepository.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = LocalAssetRepository.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1308,6 +1312,8 @@
E5890C1C29895118007A875D /* CSphereActor.h */,
E5890BF529895118007A875D /* CSwitchActor.cpp */,
E5890BDB29895118007A875D /* CSwitchActor.h */,
9419E8462B49ECB1007C50D0 /* CTeamColorAdjuster.cpp */,
9419E8452B49EC89007C50D0 /* CTeamColorAdjuster.h */,
E5890C4629895118007A875D /* CTeleporter.cpp */,
E5890BE129895118007A875D /* CTeleporter.h */,
E5890C2B29895118007A875D /* CTextActor.cpp */,
Expand Down Expand Up @@ -1337,7 +1343,6 @@
E5890C2129895118007A875D /* GoodyRecord.h */,
E5890C1029895118007A875D /* InfoMessages.h */,
E5890C4129895118007A875D /* KeyFuncs.h */,
E5890C0229895118007A875D /* LinkLoose.cpp */,
E5890BDD29895118007A875D /* Messages.h */,
E5890BE429895118007A875D /* PlayerConfig.h */,
E247DE222A0EB51B001CA630 /* PlayerRatingsSimpleElo.cpp */,
Expand Down Expand Up @@ -1419,6 +1424,7 @@
E5890C9129895118007A875D /* InternalVars.h */,
E5890C9329895118007A875D /* LevelLoader.cpp */,
E5890C9529895118007A875D /* LevelLoader.h */,
E5890C0229895118007A875D /* LinkLoose.cpp */,
E5890C9229895118007A875D /* LinkLoose.h */,
E5890C9629895118007A875D /* Parser.cpp */,
E5890C9429895118007A875D /* Parser.h */,
Expand Down Expand Up @@ -2115,6 +2121,7 @@
94924EB02B3A894900197378 /* OggFile.cpp in Sources */,
E517F09D299713DB0036B206 /* CSwitchActor.cpp in Sources */,
E517F09E299713DB0036B206 /* CTeleporter.cpp in Sources */,
9419E8482B49ECB1007C50D0 /* CTeamColorAdjuster.cpp in Sources */,
E517F09F299713DB0036B206 /* CTextActor.cpp in Sources */,
E517F0A0299713DB0036B206 /* CTriPyramidActor.cpp in Sources */,
E517F0A1299713DB0036B206 /* CUfo.cpp in Sources */,
Expand Down Expand Up @@ -2281,6 +2288,7 @@
94924EAF2B3A894900197378 /* OggFile.cpp in Sources */,
E5890EFB29895124007A875D /* darwin.mm in Sources */,
E5890CBC29895118007A875D /* CAbstractMissile.cpp in Sources */,
9419E8472B49ECB1007C50D0 /* CTeamColorAdjuster.cpp in Sources */,
E5890CD229895118007A875D /* CBall.cpp in Sources */,
E5890CC929895118007A875D /* CPlayerMissile.cpp in Sources */,
E5890F0729895124007A875D /* desccombobox.cpp in Sources */,
Expand Down
11 changes: 11 additions & 0 deletions rsrc/objects.json
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,17 @@
"attrs": {},
"index": 37,
"description": ""
},
"TeamColor": {
"type": "actor",
"attrs": {
"mask": {
"default": "allTeams",
"description": "Specifies the team(s) whose colors will be overridden by this adjuster. Usable values include `T1`, `T2`, ...`T8`, or even expressions like `T1 + T3 + T5 + T7`."
}
},
"index": 38,
"description": "Adjusts the team color value for the specified team(s)."
}
}
}
25 changes: 24 additions & 1 deletion src/assets/AssetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ std::shared_ptr<AssetStorage> AssetManager::assetStorage = _localStorage;
std::vector<std::shared_ptr<AssetRepository>> AssetManager::repositoryStack = {
_localRepo
};
std::shared_ptr<nlohmann::json> AssetManager::objectTypes = nullptr;
SimpleAssetCache<PackageManifest> AssetManager::manifestCache = {};
SimpleAssetCache<std::string> AssetManager::avarascriptCache = {};
AssetCache<nlohmann::json> AssetManager::bspCache = {};
Expand All @@ -64,6 +65,15 @@ std::vector<std::string> AssetManager::GetAvailablePackages()
return pkgs;
}

std::shared_ptr<nlohmann::json> AssetManager::GetEnumeratedObjectTypes()
{
if (objectTypes == nullptr) {
LoadEnumeratedObjectTypes();
}

return objectTypes;
}

std::optional<std::string> AssetManager::GetResolvedAlfPath(std::string relativePath)
{
// Attempt to load from packages in priority order.
Expand Down Expand Up @@ -170,6 +180,7 @@ void AssetManager::Init()
{
LoadManifest(NoPackage);
LoadScript(NoPackage);
LoadEnumeratedObjectTypes();
}

OSErr AssetManager::LoadLevel(std::string packageName, std::string relativePath, std::string &levelName)
Expand Down Expand Up @@ -219,7 +230,7 @@ bool AssetManager::PackageInStorage(std::string packageName)
return (bool)GetPackagePath(packageName);
}

std::string AssetManager::GetBasePackagePath(BasePackage basePackage) throw()
std::string AssetManager::GetBasePackagePath(BasePackage basePackage)
{
std::stringstream path;
path << baseStorage->GetRootPath();
Expand Down Expand Up @@ -298,6 +309,18 @@ std::string AssetManager::GetOggPath(MaybePackage package, int16_t id)
return GetFullPath(package, relativePath.str());
}

void AssetManager::LoadEnumeratedObjectTypes()
{
std::stringstream path;
path << baseStorage->GetRootPath() << PATHSEP << "objects.json";
std::ifstream file(path.str());
if (file.fail()) {
throw std::runtime_error("Failed to load objects.json");
} else {
objectTypes = std::make_shared<nlohmann::json>(nlohmann::json::parse(file));
}
}

void AssetManager::LoadManifest(MaybePackage package)
{
std::string path = GetManifestPath(package);
Expand Down
21 changes: 20 additions & 1 deletion src/assets/AssetManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ class AssetManager {
*/
static std::vector<std::string> GetAvailablePackages();

/**
* Get a JSON object containing descriptions of every Avarascript object that can be
* instantiated, listed in their properly enumerated order. The order is critical for
* `LinkLoose`, which maps Avarascript object names with their actual C++ types.
*
* @return the enumerated object descriptions
*/
static std::shared_ptr<nlohmann::json> GetEnumeratedObjectTypes();

/**
* Get the full filesystem path for an ALF file, if it is available.
*
Expand Down Expand Up @@ -155,6 +164,7 @@ class AssetManager {
static std::shared_ptr<AssetStorage> baseStorage;
static std::shared_ptr<AssetStorage> assetStorage;
static std::vector<std::shared_ptr<AssetRepository>> repositoryStack;
static std::shared_ptr<nlohmann::json> objectTypes;
static SimpleAssetCache<PackageManifest> manifestCache;
static SimpleAssetCache<std::string> avarascriptCache;
static AssetCache<nlohmann::json> bspCache;
Expand All @@ -168,7 +178,7 @@ class AssetManager {
* @throws std::invalid_argument Thrown when there is no path defined for the base package.
* @return the path to the base package
*/
static std::string GetBasePackagePath(BasePackage basePackage) throw();
static std::string GetBasePackagePath(BasePackage basePackage);

/**
* Get the filesystem path for the specified package, if it's available.
Expand Down Expand Up @@ -229,6 +239,15 @@ class AssetManager {
*/
static std::string GetOggPath(MaybePackage package, int16_t id);

/**
* Load the JSON file containing descriptions of every Avarascript object that can be
* instantiated, listed in their properly enumerated order. The order is critical for
* `LinkLoose`, which maps Avarascript object names with their actual C++ types.
*
* @throws std::runtime_error Thrown when the JSON file cannot be read.
*/
static void LoadEnumeratedObjectTypes();

/**
* Load the specified package's manifest file.
*
Expand Down
1 change: 0 additions & 1 deletion src/audio/CSoundHub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "CRateSound.h"
#include "CSoundMixer.h"
#include "Memory.h"
#include "Resource.h"

#include <SDL2/SDL.h>

Expand Down
1 change: 0 additions & 1 deletion src/bsp/CBSPPart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

#include "CViewParameters.h"
#include "Memory.h"
#include "Resource.h"
#include "AvaraDefines.h"

#include <fstream>
Expand Down
1 change: 0 additions & 1 deletion src/bsp/CScaledBSP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "CScaledBSP.h"

#include "Memory.h"
#include "Resource.h"
#include "AvaraGL.h"

void CScaledBSP::IScaledBSP(Fixed scale, short resId, CAbstractActor *anActor, short aPartCode) {
Expand Down
1 change: 0 additions & 1 deletion src/game/CAbstractActor.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "FastMat.h"
#include "LevelLoader.h"
#include "RayHit.h"
#include "Resource.h"

// define ENABLE_FPS_DEBUG in files where you want FPS_DEBUG output, BEFORE including this header
#ifdef ENABLE_FPS_DEBUG
Expand Down
3 changes: 3 additions & 0 deletions src/game/CAvaraApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "CAvaraApp.h"

#include "AssetManager.h"
#include "ColorManager.h"
#include "AvaraGL.h"
#include "AvaraScoreInterface.h"
#include "AvaraTCP.h"
Expand Down Expand Up @@ -270,6 +271,8 @@ OSErr CAvaraAppImpl::LoadLevel(std::string set, std::string levelTag, CPlayerMan
gCurrentGame = itsGame.get();
itsGame->loadedSet = set;

ColorManager::resetOverrides();

std::string levelName;
OSErr result = AssetManager::LoadLevel(set, levelTag, levelName);

Expand Down
1 change: 0 additions & 1 deletion src/game/CAvaraGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
//#include "Sound.h"
#include "CHUD.h"
#include "Preferences.h"
#include "Resource.h"
#include "ARGBColor.h"
#include "Debug.h"

Expand Down
36 changes: 36 additions & 0 deletions src/game/CTeamColorAdjuster.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "CTeamColorAdjuster.h"

#include "ARGBColor.h"
#include "AvaraDefines.h"
#include "CAvaraApp.h"
#include "ColorManager.h"
#include "Preferences.h"

void CTeamColorAdjuster::BeginScript() {
CAbstractActor::BeginScript();

ProgramVariable(iMask, -1);
}

CAbstractActor *CTeamColorAdjuster::EndScript() {
CAbstractActor::EndScript();

if (!itsGame->itsApp->Boolean(kIgnoreLevelCustomColorsTag)) {
colorMask = ReadLongVar(iMask);

ARGBColor newColor = GetPixelColor();

// Currently unused, but theoretically should be supported. (We don't do highlighting yet.)
// auto newHighlightColor = GetOtherPixelColor();

for (uint8_t i = 1; i <= kMaxTeamColors; i++) {
uint16_t teamMask = 1 << i;
if (colorMask & teamMask) {
ColorManager::overrideTeamColor(i, newColor);
}
}
}

Dispose();
return NULL;
}
10 changes: 10 additions & 0 deletions src/game/CTeamColorAdjuster.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once
#include "CAbstractActor.h"

class CTeamColorAdjuster : public CAbstractActor {
public:
long colorMask;

void BeginScript();
virtual CAbstractActor *EndScript();
};
1 change: 0 additions & 1 deletion src/game/CWorldShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "CAvaraApp.h"
#include "CAvaraGame.h"
#include "CViewParameters.h"
#include "Resource.h"
#include "CBSPPart.h"
#include "AvaraGL.h"

Expand Down
1 change: 1 addition & 0 deletions src/gui/CApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void CApplication::BroadcastCommand(int theCommand) {

void CApplication::PrefChanged(std::string name) {
ColorManager::refresh(this);

for (auto win : windowList) {
win->PrefChanged(name);
}
Expand Down
17 changes: 17 additions & 0 deletions src/gui/ColorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ std::string ColorManager::teamColorNames[kMaxTeamColors + 1] = {
"White"
};

std::optional<ARGBColor> ColorManager::teamColorOverrides[kMaxTeamColors + 1] = {};
std::optional<ARGBColor> ColorManager::teamTextColorOverrides[kMaxTeamColors + 1] = {};

ARGBColor ColorManager::messageColors[3] = {
0xffffffff,
0xff92ebe9,
Expand Down Expand Up @@ -300,6 +303,13 @@ void ColorManager::setMissileLaunchedColor(ARGBColor color) {
ColorManager::missileLaunchedColor = color;
}

void ColorManager::overrideTeamColor(uint8_t num, ARGBColor color) {
if (num <= kMaxTeamColors) {
ColorManager::teamColorOverrides[num] = color;
ColorManager::teamTextColorOverrides[num] = 0xff333333;
}
}

void ColorManager::refresh(CApplication *app) {
ColorManager::setColorBlind(app->Get(kColorBlindMode));
ColorManager::setHUDColor(ARGBColor::Parse(app->String(kHUDColor)).value_or(0xff03f5f5));
Expand All @@ -308,3 +318,10 @@ void ColorManager::refresh(CApplication *app) {
ColorManager::setHUDCriticalColor(ARGBColor::Parse(app->String(kHUDCriticalColor)).value_or(0xfffa1313));
ColorManager::setHUDAlpha(app->Get(kHUDAlpha));
}

void ColorManager::resetOverrides() {
for (uint8_t i = 0; i < kMaxTeamColors + 1; i++) {
ColorManager::teamColorOverrides[i] = {};
ColorManager::teamTextColorOverrides[i] = {};
}
}
15 changes: 12 additions & 3 deletions src/gui/ColorManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,17 @@ class ColorManager {

static inline std::optional<ARGBColor> getTeamColor(uint8_t num) {
return (num <= kMaxTeamColors)
? teamColors[num]
? (teamColorOverrides[num])
? teamColorOverrides[num]
: teamColors[num]
: std::optional<ARGBColor>{};
}

static inline std::optional<ARGBColor> getTeamTextColor(uint8_t num) {
return (num <= kMaxTeamColors)
? teamTextColors[num]
? (teamTextColorOverrides[num])
? teamTextColorOverrides[num]
: teamTextColors[num]
: std::optional<ARGBColor>{};
}

Expand All @@ -170,7 +174,7 @@ class ColorManager {
}

static inline ARGBColor getPingColor(uint8_t num) {
return pingColors[num];
return pingColors[num];
}

static inline float getHUDAlpha() {
Expand All @@ -186,7 +190,10 @@ class ColorManager {
static void setMissileArmedColor(ARGBColor color);
static void setMissileLaunchedColor(ARGBColor color);

static void overrideTeamColor(uint8_t num, ARGBColor color);

static void refresh(CApplication *app);
static void resetOverrides();
private:
ColorManager() {}

Expand Down Expand Up @@ -222,6 +229,8 @@ class ColorManager {
static ARGBColor teamColors[kMaxTeamColors + 1];
static ARGBColor teamTextColors[kMaxTeamColors + 1];
static std::string teamColorNames[kMaxTeamColors + 1];
static std::optional<ARGBColor> teamColorOverrides[kMaxTeamColors + 1];
static std::optional<ARGBColor> teamTextColorOverrides[kMaxTeamColors + 1];
static ARGBColor messageColors[3];
static ARGBColor pingColors[3];
};
Loading
Loading