File: TeamHandler.cpp

package info (click to toggle)
spring 106.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 55,316 kB
  • sloc: cpp: 543,954; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (229 lines) | stat: -rw-r--r-- 6,630 bytes parent folder | download | duplicates (3)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "TeamHandler.h"

#include <cstring>

#include "Game/GameSetup.h"
#include "Sim/Misc/GlobalConstants.h"
#include "Sim/Misc/GlobalSynced.h"


CR_BIND(CTeamHandler, )

CR_REG_METADATA(CTeamHandler, (
	CR_MEMBER(gaiaTeamID),
	CR_MEMBER(gaiaAllyTeamID),
	CR_MEMBER(teams),
	CR_MEMBER(allyTeams)
))


// needs to be available as early as PreGame
CTeamHandler teamHandler;


void CTeamHandler::LoadFromSetup(const CGameSetup* setup)
{
	// must start from a blank slate
	assert(teams.empty());
	assert(allyTeams.empty());

	assert(!setup->GetTeamStartingDataCont().empty());
	assert(setup->GetTeamStartingDataCont().size() <= MAX_TEAMS);
	assert(setup->GetAllyStartingDataCont().size() <= MAX_TEAMS);

	teams.reserve(setup->GetTeamStartingDataCont().size() + 1); // +1 for Gaia
	teams.resize(setup->GetTeamStartingDataCont().size());

	allyTeams = setup->GetAllyStartingDataCont();

	const int numTeams = teams.size() + int(gs->useLuaGaia);
	const int maxUnitsPerTeam = std::min(setup->maxUnitsPerTeam, int(MAX_UNITS / numTeams));

	for (size_t i = 0; i < teams.size(); ++i) {
		teams[i].teamNum = i;
		teams[i] = setup->GetTeamStartingDataCont()[i];

		// all non-Gaia teams (within one allyteam) get and maintain the same unit-limit
		// (because of this it would be better treated as a pool owned by class AllyTeam)
		teams[i].SetMaxUnits(maxUnitsPerTeam);

		assert(teams[i].teamAllyteam >=                0);
		assert(teams[i].teamAllyteam <  allyTeams.size());
	}

	if (gs->useLuaGaia) {
		// Gaia adjustments
		gaiaTeamID = static_cast<int>(teams.size());
		gaiaAllyTeamID = static_cast<int>(allyTeams.size());

		// setup the Gaia team
		teams.emplace_back();
		CTeam& gaia = teams.back();

		gaia.teamNum = gaiaTeamID;
		gaia.color[0] = 255;
		gaia.color[1] = 255;
		gaia.color[2] = 255;
		gaia.color[3] = 255;
		gaia.gaia = true;
		gaia.SetMaxUnits(MAX_UNITS - ((teams.size() - 1) * teams[0].GetMaxUnits()));
		gaia.SetStartPos(ZeroVector);
		gaia.teamAllyteam = gaiaAllyTeamID;

		assert((((teams.size() - 1) * teams[0].GetMaxUnits()) + gaia.GetMaxUnits()) == MAX_UNITS);

		// make every existing AT an enemy of Gaia
		for (::AllyTeam& allyTeam: allyTeams) {
			allyTeam.allies.push_back(false);
		}


		allyTeams.emplace_back();
		::AllyTeam& allyteam = allyTeams.back();

		allyteam.allies.resize(allyTeams.size() + 1, false); // make Gaia every AT's enemy
		allyteam.allies[gaiaAllyTeamID] = true; // set Gaia to be at peace with itself
	}
}

void CTeamHandler::SetDefaultStartPositions(const CGameSetup* setup)
{
	if (setup->startPosType != CGameSetup::StartPos_ChooseInGame)
		return;

	for (int a = 0; a < ActiveTeams(); ++a) {
		CTeam& team = teams[a];

		if (team.gaia)
			continue;
		if (team.HasValidStartPos())
			continue;

		// if the player did not choose a start position (eg. if
		// the game was force-started by the host before sending
		// any), silently generate one for him
		// TODO: notify Lua of this also?
		team.SetDefaultStartPos();
	}
}

void CTeamHandler::GameFrame(int frameNum)
{
	if ((frameNum % TEAM_SLOWUPDATE_RATE) != 0)
		return;

	for (int a = 0; a < ActiveTeams(); ++a) {
		teams[a].ResetResourceState();
	}
	for (int a = 0; a < ActiveTeams(); ++a) {
		teams[a].SlowUpdate();
	}
}



unsigned int CTeamHandler::GetNumTeamsInAllyTeam(unsigned int allyTeamNum, bool countDeadTeams) const
{
	unsigned int numTeams = 0;

	for (unsigned int teamNum = 0; teamNum < teamHandler.ActiveTeams(); teamNum++) {
		if (AllyTeam(teamNum) != allyTeamNum)
			continue;
		if (teams[teamNum].isDead && !countDeadTeams)
			continue;

		numTeams += 1;
	}

	return numTeams;
}

void CTeamHandler::UpdateTeamUnitLimitsPreSpawn(int liveTeamNum)
{
	CTeam* liveTeam = &teams[liveTeamNum];
	CTeam* tempTeam = nullptr;

	// will be set to true immediately after we return
	assert(liveTeam->isDead);

	// Gaia team is alone in its allyteam and cannot really spawn
	if (liveTeamNum == gaiaTeamID)
		return;

	// count the number of remaining (non-dead) teams in <liveTeam>'s allyteam, EXCLUDING us
	const unsigned int numRemainingActiveTeams = GetNumTeamsInAllyTeam(AllyTeam(liveTeamNum), false);

	if (numRemainingActiveTeams == 0) {
		// set default since we are the only team in our allyteam now
		liveTeam->SetMaxUnits(std::min(gameSetup->maxUnitsPerTeam, int(MAX_UNITS / teams.size())));
		return;
	}

	// reduce the limit of each team in our allyteam uniformly
	for (unsigned int tempTeamNum = 0; tempTeamNum < teams.size(); tempTeamNum++) {
		if (tempTeamNum == liveTeamNum)
			continue;
		if (AllyTeam(tempTeamNum) != AllyTeam(liveTeamNum))
			continue;
		if (Team(tempTeamNum)->isDead)
			continue;

		tempTeam = &teams[tempTeamNum];
		tempTeam->SetMaxUnits((tempTeam->GetMaxUnits() * numRemainingActiveTeams) / (numRemainingActiveTeams + 1));
	}

	assert(tempTeam != nullptr);
	liveTeam->SetMaxUnits(tempTeam->GetMaxUnits());
}

void CTeamHandler::UpdateTeamUnitLimitsPreDeath(int deadTeamNum)
{
	CTeam* deadTeam = &teams[deadTeamNum];
	CTeam* tempTeam = nullptr;

	// will be set to false immediately after we return
	assert(!deadTeam->isDead);

	// Gaia team is alone in its allyteam and cannot really die
	if (deadTeamNum == gaiaTeamID)
		return;

	// count the number of remaining (non-dead) teams in <deadTeam>'s allyteam, INCLUDING us
	const unsigned int numRemainingActiveTeams = GetNumTeamsInAllyTeam(AllyTeam(deadTeamNum), false);

	if (numRemainingActiveTeams == 1) {
		// no more zombie unit production after death
		deadTeam->SetMaxUnits(0);
		return;
	}

	// redistribute <deadTeam>'s unit-limit over those teams uniformly
	//
	// assume an allyteam of 5 teams, each with a unit-limit of 500
	// whenever one of these teams dies, its limit is redistributed
	// as follows (integer division means some units are lost):
	//
	//   5 x  500 = 2500 --> 4 x ( 500 + ( 500/4)= 125 =  625)=2500
	//   4 x  625 = 2500 --> 3 x ( 625 + ( 625/3)= 208 =  833)=2499
	//   3 x  833 = 2499 --> 2 x ( 833 + ( 833/2)= 416 = 1249)=2498
	//   2 x 1249 = 2498 --> 1 x (1249 + (1249/1)=1249 = 2498)=2498
	for (unsigned int tempTeamNum = 0; tempTeamNum < teams.size(); tempTeamNum++) {
		if (tempTeamNum == deadTeamNum)
			continue;
		if (AllyTeam(tempTeamNum) != AllyTeam(deadTeamNum))
			continue;
		if (teams[tempTeamNum].isDead)
			continue;

		assert(teams[tempTeamNum].GetMaxUnits() == deadTeam->GetMaxUnits());

		tempTeam = &teams[tempTeamNum];
		tempTeam->SetMaxUnits((tempTeam->GetMaxUnits() * numRemainingActiveTeams) / (numRemainingActiveTeams - 1));
	}

	assert(tempTeam != nullptr);
	deadTeam->SetMaxUnits(0);
}