File: Player.cpp

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (247 lines) | stat: -rwxr-xr-x 5,691 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include <assert.h>

#include "System/mmgr.h"

#include "ExternalAI/SkirmishAIHandler.h"
#include "Game/Player.h"
#include "Game/PlayerHandler.h"
#include "Game/Camera.h"
#include "Game/CameraHandler.h"
#include "Game/GlobalUnsynced.h"
#include "Game/SelectedUnits.h"
#include "Game/UI/MouseHandler.h"
#include "Game/UI/UnitTracker.h"
#include "Lua/LuaRules.h"
#include "Lua/LuaUI.h"
#include "Sim/Misc/TeamHandler.h"
#include "Sim/Misc/GlobalSynced.h"
#include "Sim/Units/Unit.h"
#include "Sim/Units/UnitHandler.h"
#include "System/myMath.h"
#include "System/EventHandler.h"
#include "System/Log/ILog.h"

CR_BIND(CPlayer,);

CR_REG_METADATA(CPlayer, (
	CR_MEMBER(name),
	CR_MEMBER(countryCode),
	CR_MEMBER(rank),
	CR_MEMBER(spectator),
	CR_MEMBER(team),

	CR_MEMBER(active),
	CR_MEMBER(playerNum),
//	CR_MEMBER(readyToStart),
//	CR_MEMBER(cpuUsage),
//	CR_MEMBER(ping),
//	CR_MEMBER(currentStats),

//	CR_MEMBER(controlledTeams),
	CR_RESERVED(32)
));


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPlayer::CPlayer()
	: PlayerBase()
	, active(false)
	, playerNum(-1)
	, ping(0)
{
	fpsController.SetControllerPlayer(this);
}



void CPlayer::SetControlledTeams()
{
	controlledTeams.clear();

	if (gs->godMode) {
		// anyone can control any unit
		for (int t = 0; t < teamHandler->ActiveTeams(); t++) {
			controlledTeams.insert(t);
		}
		return;
	}

	if (!spectator) {
		// my team
		controlledTeams.insert(team);
	}

	// AI teams
	const CSkirmishAIHandler::id_ai_t aiIds = skirmishAIHandler.GetAllSkirmishAIs();
	for (CSkirmishAIHandler::id_ai_t::const_iterator ai = aiIds.begin(); ai != aiIds.end(); ++ai) {
		const bool isHostedByUs = (ai->second.hostPlayer == playerNum);
		if (isHostedByUs) {
			controlledTeams.insert(ai->second.team);
		}
	}
}


void CPlayer::UpdateControlledTeams()
{
	for (int p = 0; p < playerHandler->ActivePlayers(); p++) {
		CPlayer* player = playerHandler->Player(p);
		if (player) {
			player->SetControlledTeams();
		}
	}
}


void CPlayer::StartSpectating()
{
	if (spectator) {
		return;
	}

	spectator = true;

	if (gu->myPlayerNum == this->playerNum) {
		// HACK: unsynced code should just listen for the PlayerChanged event
		gu->spectating           = true;
		gu->spectatingFullView   = true;
		gu->spectatingFullSelect = true;

		CLuaUI::UpdateTeams();
		selectedUnits.ClearSelected();
		unitTracker.Disable();
	}

	StopControllingUnit();
	eventHandler.PlayerChanged(playerNum);
}

void CPlayer::JoinTeam(int newTeam)
{
	// a player that joins a team always stops spectating
	spectator = false;
	team = newTeam;

	if (gu->myPlayerNum == this->playerNum) {
		// HACK: see StartSpectating
		gu->myPlayingTeam = gu->myTeam = newTeam;
		gu->myPlayingAllyTeam = gu->myAllyTeam = teamHandler->AllyTeam(gu->myTeam);

		gu->spectating           = false;
		gu->spectatingFullView   = false;
		gu->spectatingFullSelect = false;

		CLuaUI::UpdateTeams();
		selectedUnits.ClearSelected();
		unitTracker.Disable();
	}

	eventHandler.PlayerChanged(playerNum);
}

void CPlayer::GameFrame(int frameNum)
{
	if (!active || (fpsController.GetControllee() == NULL)) {
		return;
	}

	fpsController.Update();
}



void CPlayer::StartControllingUnit()
{
	CUnit* curControlleeUnit = fpsController.GetControllee();
	CUnit* newControlleeUnit = NULL;

	if (curControlleeUnit != NULL) {
		// player released control
		StopControllingUnit();
	} else {
		// player took control
		const std::vector<int>& ourSelectedUnits = selectedUnits.netSelected[this->playerNum];

		if (ourSelectedUnits.empty()) {
			return;
		}

		// pick the first unit we have selected
		newControlleeUnit = uh->GetUnit(ourSelectedUnits[0]);

		if (newControlleeUnit == NULL || newControlleeUnit->weapons.empty()) {
			return;
		}

		if (newControlleeUnit->fpsControlPlayer != NULL) {
			if (this->playerNum == gu->myPlayerNum) {
				LOG_L(L_WARNING,
						"player %d (%s) is already controlling unit %d",
						newControlleeUnit->fpsControlPlayer->playerNum,
						newControlleeUnit->fpsControlPlayer->name.c_str(),
						newControlleeUnit->id);
			}

			return;
		}

		if (luaRules == NULL || luaRules->AllowDirectUnitControl(this->playerNum, newControlleeUnit)) {
			newControlleeUnit->fpsControlPlayer = this;
			fpsController.SetControlleeUnit(newControlleeUnit);
			selectedUnits.ClearNetSelect(this->playerNum);

			if (this->playerNum == gu->myPlayerNum) {
				// update the unsynced state
				selectedUnits.ClearSelected();

				gu->fpsMode = true;
				mouse->wasLocked = mouse->locked;

				if (!mouse->locked) {
					mouse->locked = true;
					mouse->HideMouse();
				}
				camHandler->PushMode();
				camHandler->SetCameraMode(0);
			}
		}
	}
}

void CPlayer::StopControllingUnit()
{
	if (fpsController.GetControllee() == NULL || mouse == NULL) {
		return;
	}

	CPlayer* that = gu->GetMyPlayer();
	CUnit* thatUnit = that->fpsController.GetControllee();
	CUnit* thisUnit = this->fpsController.GetControllee();

	// note: probably better to issue CMD_STOP via thisUnit->commandAI
	thisUnit->AttackUnit(NULL, false, true);
	thisUnit->fpsControlPlayer = NULL;
	fpsController.SetControlleeUnit(NULL);
	selectedUnits.ClearNetSelect(this->playerNum);

	if (thatUnit == thisUnit) {
		// update the unsynced state
		selectedUnits.ClearSelected();

		gu->fpsMode = false;
		assert(gu->myPlayerNum == this->playerNum);

		// switch back to the camera we were using before
		camHandler->PopMode();

		if (mouse->locked && !mouse->wasLocked) {
			mouse->locked = false;
			mouse->ShowMouse();
		}
	}
}