1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "TeamHighlight.h"
#include "ExternalAI/SkirmishAIHandler.h"
#include "Game/GlobalUnsynced.h"
#include "Game/Player.h"
#include "Game/PlayerHandler.h"
#include "Sim/Misc/TeamHandler.h"
#include "Sim/Misc/GlobalConstants.h"
#include "Sim/Misc/GlobalSynced.h"
#include "System/Config/ConfigHandler.h"
#include "System/GlobalConfig.h"
#include <climits>
bool CTeamHighlight::highlight = false;
std::map<int, int> CTeamHighlight::oldColors;
void CTeamHighlight::Enable(unsigned currentTime) {
if (highlight) {
for (int i = 0; i < teamHandler->ActiveTeams(); ++i) {
CTeam* t = teamHandler->Team(i);
if (t->highlight > 0.0f) {
oldColors[i] = *(int *)t->color;
float s = (float)(currentTime & 255) * 4.0f / 255.0f;
int c =(int)(255.0f * ((s > 2.0f) ? 3.0f - s : s - 1.0f));
c *= t->highlight;
for (int n = 0; n < 3; ++n) {
t->color[n] = std::max(0, std::min(t->color[n] + c , 255));
}
}
}
}
}
void CTeamHighlight::Disable() {
if (!oldColors.empty()) {
for (std::map<int, int>::iterator i = oldColors.begin(); i != oldColors.end(); ++i) {
CTeam* team = teamHandler->Team((*i).first);
*(int*)team->color = (*i).second;
}
oldColors.clear();
}
}
void CTeamHighlight::Update(int frameNum) {
if (frameNum & (TEAM_SLOWUPDATE_RATE - 1))
return;
bool hl = false;
if ((globalConfig->teamHighlight == HIGHLIGHT_PLAYERS && !gu->spectatingFullView) || globalConfig->teamHighlight == HIGHLIGHT_ALL) {
const int maxhl = 1000 * (globalConfig->networkTimeout + 1);
for (int ti = 0; ti < teamHandler->ActiveTeams(); ++ti) {
CTeam* t = teamHandler->Team(ti);
float teamhighlight = 0.0f;
if (t->gaia) { continue; }
if (t->isDead) { continue; }
if (t->units.empty()) { continue; }
if (!skirmishAIHandler.GetSkirmishAIsInTeam(ti).empty()) { continue; }
if (!gu->spectatingFullView && !teamHandler->AlliedTeams(gu->myTeam, ti)) { continue; }
int minPing = INT_MAX;
bool hasPlayers = false;
for (int pi = 0; pi < playerHandler->ActivePlayers(); ++pi) {
CPlayer* p = playerHandler->Player(pi);
if (!p->active) { continue; }
if (p->spectator) { continue; }
if ((p->team != ti)) { continue; }
hasPlayers = true;
if (p->ping != PATHING_FLAG && p->ping >= 0) {
const int speed = GAME_SPEED * gs->speedFactor;
const int ping = (p->ping * 1000) / speed;
minPing = std::min(ping, minPing);
}
}
if (!hasPlayers || t->leader < 0) {
teamhighlight = 1.0f;
} else if (minPing != INT_MAX && minPing > 1000) {
teamhighlight = std::max(0, std::min(minPing, maxhl)) / float(maxhl);
}
if (teamhighlight > 0.0f) {
hl = true;
}
*(volatile float *)&t->highlight = teamhighlight;
}
}
*(volatile bool *)&highlight = hl;
}
|