File: TeamHighlight.cpp

package info (click to toggle)
spring 104.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,512 kB
  • sloc: cpp: 391,093; ansic: 79,943; python: 12,356; java: 12,201; awk: 5,889; sh: 1,826; xml: 655; makefile: 486; perl: 405; php: 211; objc: 194; sed: 2
file content (106 lines) | stat: -rw-r--r-- 2,805 bytes parent folder | download
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
101
102
103
104
105
106
/* 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/Players/Player.h"
#include "Game/Players/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 "System/UnorderedMap.hpp"

#include <algorithm>
#include <climits>


bool CTeamHighlight::highlight = false;
static spring::unordered_map<int, int> oldColors;

void CTeamHighlight::Enable(unsigned currentTime)
{
	if (!highlight)
		return;

	for (int i = 0; i < teamHandler->ActiveTeams(); ++i) {
		CTeam* t = teamHandler->Team(i);

		if (t->highlight <= 0.0f)
			continue;

		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())
		return;

	for (auto 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))
		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) {
					minPing = std::min(p->ping, minPing);
				}
			}
			if (!hasPlayers || !t->HasLeader()) {
				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;
}