File: GameSetupDrawer.cpp

package info (click to toggle)
spring 98.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 41,928 kB
  • ctags: 60,665
  • sloc: cpp: 356,167; ansic: 39,434; python: 12,228; java: 12,203; awk: 5,856; sh: 1,719; xml: 997; perl: 405; php: 253; objc: 194; makefile: 72; sed: 2
file content (187 lines) | stat: -rw-r--r-- 5,164 bytes parent folder | download | duplicates (2)
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "GameSetupDrawer.h"

#include "KeyBindings.h"
#include "StartPosSelecter.h"
#include "Game/CameraHandler.h"
#include "Game/GameSetup.h"
#include "Game/GlobalUnsynced.h"
#include "Game/Players/Player.h"
#include "Game/Players/PlayerHandler.h"
#include "Net/GameServer.h"
#include "Sim/Misc/GlobalSynced.h"
#include "Sim/Misc/TeamHandler.h"
#include "Rendering/Fonts/glFont.h"
#include "Net/Protocol/NetProtocol.h"
#include "System/Config/ConfigHandler.h"
#include "System/EventHandler.h"

#include <cassert>
#include <string>
#include <map>


GameSetupDrawer* GameSetupDrawer::instance = NULL;


void GameSetupDrawer::Enable()
{
	assert(instance == NULL);
	assert(gameSetup);

	instance = new GameSetupDrawer();
}


void GameSetupDrawer::Disable()
{
	delete instance;
	instance = NULL;
}

void GameSetupDrawer::StartCountdown(unsigned time)
{
	if (instance) {
		instance->lastTick = spring_gettime(); //FIXME
		instance->readyCountdown = spring_msecs(time);
	}
}


GameSetupDrawer::GameSetupDrawer():
	readyCountdown(spring_notime),
	lastTick(spring_notime)
{
	if (gameSetup->startPosType == CGameSetup::StartPos_ChooseInGame && !gameSetup->hostDemo) {
		new CStartPosSelecter();
	}
	lctrl_pressed = false;
}

GameSetupDrawer::~GameSetupDrawer()
{
}


void GameSetupDrawer::Draw()
{
	if (readyCountdown > spring_nulltime) {
		readyCountdown -= (spring_gettime() - lastTick);
		lastTick = spring_gettime();

		if (readyCountdown <= spring_nulltime) {
			GameSetupDrawer::Disable();
			return; // *this is deleted!
		}
	}

	std::map<int, std::string> playerStates;
	std::string startState = "Unknown state.";

	if (readyCountdown > spring_nulltime) {
		startState = "Starting in " + IntToString(readyCountdown.toSecsi(), "%i");
	} else if (!playerHandler->Player(gu->myPlayerNum)->spectator && !playerHandler->Player(gu->myPlayerNum)->IsReadyToStart()) {
		startState = "Choose start pos";
	} else if (gameServer) {
		// we are the host and can get the show on the road by force
		const CKeyBindings::HotkeyList& fsKeys = keyBindings->GetHotkeys("forcestart");
		const std::string fsKey = fsKeys.empty() ? "<none>" : *fsKeys.begin();
		startState = std::string("Waiting for players, press ") + fsKey + " to force start";
	} else {
		startState = "Waiting for players";
	}

	const unsigned int numPlayers = playerHandler->ActivePlayers();

	// not the most efficent way to do this, but who cares?
	for (unsigned int a = 0; a < numPlayers; a++) {
		const CPlayer* player = playerHandler->Player(a);

		if (!player->active) {
			// player does not become active until we receive NETMSG_PLAYERNAME
			playerStates[a] = "missing";
		} else if (!player->spectator && !player->IsReadyToStart()) {
			playerStates[a] = "notready";
		} else {
			playerStates[a] = "ready";
		}
	}

	// if choosing in-game, selector remains non-NULL
	// so long as a position has not been picked yet
	// and deletes itself afterwards
	bool playerHasReadied = (CStartPosSelecter::GetSelector() == NULL);

	if (eventHandler.GameSetup(startState, playerHasReadied, playerStates)) {
		if (CStartPosSelecter::GetSelector() != NULL) {
			CStartPosSelecter::GetSelector()->ShowReadyBox(false);

			if (playerHasReadied) {
				// either we were ready before or a client
				// listening to the GameSetup event forced
				// us to be
				CStartPosSelecter::GetSelector()->Ready(true);
			}
		}

		// LuaUI says it will do the rendering
		return;
	}

	// LuaUI doesn't want to draw, keep showing the box
	if (CStartPosSelecter::GetSelector() != NULL) {
		CStartPosSelecter::GetSelector()->ShowReadyBox(true);
	}

	font->Begin();
	font->SetColors(); // default
	font->glPrint(0.3f, 0.7f, 1.0f, FONT_OUTLINE | FONT_SCALE | FONT_NORM, startState);

	for (unsigned int a = 0; a <= numPlayers; a++) {
		static const float4      red(1.0f, 0.2f, 0.2f, 1.0f);
		static const float4    green(0.2f, 1.0f, 0.2f, 1.0f);
		static const float4   yellow(0.8f, 0.8f, 0.2f, 1.0f);
		static const float4    white(1.0f, 1.0f, 1.0f, 1.0f);
		static const float4     cyan(0.0f, 0.9f, 0.9f, 1.0f);
		static const float4 lightred(1.0f, 0.5f, 0.5f, 1.0f);

		const float fontScale = 1.0f;
		const float fontSize  = fontScale * font->GetSize();
		const float yScale = fontSize * font->GetLineHeight() * globalRendering->pixelY;
		// note: list is drawn in reverse order, last player first
		const float yPos = 0.5f - (0.5f * yScale * numPlayers) + (yScale * a);
		const float xPos = 10.0f * globalRendering->pixelX;

		const CPlayer* player = NULL;
		const float4* color = NULL;

		std::string name;

		if (a == numPlayers) {
			color = &white;
			name = "Players:";
		} else {
			player = playerHandler->Player(a);
			name = player->name;

			if (player->spectator) {
				if (!player->active) {
					color = &lightred;
				} else {
					color = &cyan;
				}
			} else if (!player->active) {
				color = &red;
			} else if (!player->IsReadyToStart()) {
				color = &yellow;
			} else {
				color = &green;
			}
		}

		font->SetColors(color, NULL);
		font->glPrint(xPos, yPos, fontSize, FONT_OUTLINE | FONT_NORM, name);
	}
	font->End();
}