File: StartPosSelecter.cpp

package info (click to toggle)
spring 106.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,260 kB
  • sloc: cpp: 543,946; 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 (245 lines) | stat: -rw-r--r-- 8,372 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */


#include "StartPosSelecter.h"
#include "MouseHandler.h"
#include "Game/Camera.h"
#include "Game/GameSetup.h"
#include "Game/GlobalUnsynced.h"
#include "Game/InMapDraw.h"
#include "Game/Players/Player.h"
#include "Map/Ground.h"
#include "Map/ReadMap.h"
#include "Rendering/GL/myGL.h"
#include "Rendering/GL/glExtra.h"
#include "Rendering/Fonts/glFont.h"
#include "Net/Protocol/NetProtocol.h"
#include "Sim/Misc/TeamHandler.h"


CStartPosSelecter* CStartPosSelecter::selector = nullptr;


CStartPosSelecter::CStartPosSelecter() : CInputReceiver(BACK)
{
	showReadyBox = true;
	startPosSet = false;

	readyBox.x1 = 0.71f;
	readyBox.y1 = 0.72f;
	readyBox.x2 = 0.81f;
	readyBox.y2 = 0.76f;

	selector = this;
}

CStartPosSelecter::~CStartPosSelecter()
{
	selector = nullptr;
}


bool CStartPosSelecter::Ready(bool luaForcedReady)
{
	if (!gs->PreSimFrame()) {
		delete this;
		return true;
	}

	const CTeam* mt = teamHandler.Team(gu->myTeam);
	const float3* sp = nullptr;

	// player did not set a startpos yet, so do not let
	// him ready up if he clicked on the ready-box first
	//
	// do allow in the special case where player already
	// sent a RDYSTATE_UPDATED and then rejoined the game
	if (!mt->HasValidStartPos()) {
		if (!startPosSet && !luaForcedReady)
			return false;

		sp = &setStartPos;
	} else {
		sp = &mt->GetStartPos();
	}

	if (luaForcedReady) {
		clientNet->Send(CBaseNetProtocol::Get().SendStartPos(gu->myPlayerNum, gu->myTeam, CPlayer::PLAYER_RDYSTATE_FORCED, sp->x, sp->y, sp->z));
	} else {
		clientNet->Send(CBaseNetProtocol::Get().SendStartPos(gu->myPlayerNum, gu->myTeam, CPlayer::PLAYER_RDYSTATE_READIED, sp->x, sp->y, sp->z));
	}

	delete this;
	return true;
}


bool CStartPosSelecter::MousePress(int x, int y, int button)
{
	const float mx = MouseX(x);
	const float my = MouseY(y);

	if ((showReadyBox && InBox(mx, my, readyBox)) || !gs->PreSimFrame())
		return (!Ready(false));

	const float dist = CGround::LineGroundCol(camera->GetPos(), camera->GetPos() + mouse->dir * camera->GetFarPlaneDist() * 1.4f, false);

	if (dist < 0.0f)
		return true;

	inMapDrawer->SendErase(setStartPos);
	startPosSet = true;
	setStartPos = camera->GetPos() + mouse->dir * dist;
	clientNet->Send(CBaseNetProtocol::Get().SendStartPos(gu->myPlayerNum, gu->myTeam, CPlayer::PLAYER_RDYSTATE_UPDATED, setStartPos.x, setStartPos.y, setStartPos.z));

	return true;
}


#if 0
void CStartPosSelecter::DrawStartBox(GL::RenderDataBufferC* buffer, Shader::IProgramObject* shader) const
{
	glAttribStatePtr->EnableDepthTest();

	const std::vector<AllyTeam>& allyStartData = CGameSetup::GetAllyStartingData();
	const AllyTeam& myStartData = allyStartData[gu->myAllyTeam];

	const float by = myStartData.startRectTop * mapDims.mapy * SQUARE_SIZE;
	const float bx = myStartData.startRectLeft * mapDims.mapx * SQUARE_SIZE;

	const float dy = (myStartData.startRectBottom - myStartData.startRectTop ) * mapDims.mapy * SQUARE_SIZE / 10;
	const float dx = (myStartData.startRectRight  - myStartData.startRectLeft) * mapDims.mapx * SQUARE_SIZE / 10;

	// draw starting-rectangle restrictions
	for (int a = 0; a < 10; ++a) {
		float3 pos1(bx + (a    ) * dx, 0.0f, by); // tl
		float3 pos2(bx + (a + 1) * dx, 0.0f, by); // tr

		pos1.y = CGround::GetHeightAboveWater(pos1.x, pos1.z, false);
		pos2.y = CGround::GetHeightAboveWater(pos2.x, pos2.z, false);

		buffer->SafeAppend({pos1                    , {0.2f, 0.8f, 0.2f, 0.5f}});
		buffer->SafeAppend({pos2                    , {0.2f, 0.8f, 0.2f, 0.5f}});
		buffer->SafeAppend({pos2 + UpVector * 100.0f, {0.2f, 0.8f, 0.2f, 0.5f}});

		buffer->SafeAppend({pos2 + UpVector * 100.0f, {0.2f, 0.8f, 0.2f, 0.5f}});
		buffer->SafeAppend({pos1 + UpVector * 100.0f, {0.2f, 0.8f, 0.2f, 0.5f}});
		buffer->SafeAppend({pos1                    , {0.2f, 0.8f, 0.2f, 0.5f}});


		pos1 = float3(bx + (a    ) * dx, 0.0f, by + dy * 10.0f);
		pos2 = float3(bx + (a + 1) * dx, 0.0f, by + dy * 10.0f);
		pos1.y = CGround::GetHeightAboveWater(pos1.x, pos1.z, false);
		pos2.y = CGround::GetHeightAboveWater(pos2.x, pos2.z, false);

		buffer->SafeAppend({pos1                    , {0.2f, 0.8f, 0.2f, 0.5f}});
		buffer->SafeAppend({pos2                    , {0.2f, 0.8f, 0.2f, 0.5f}});
		buffer->SafeAppend({pos2 + UpVector * 100.0f, {0.2f, 0.8f, 0.2f, 0.5f}});

		buffer->SafeAppend({pos2 + UpVector * 100.0f, {0.2f, 0.8f, 0.2f, 0.5f}});
		buffer->SafeAppend({pos1 + UpVector * 100.0f, {0.2f, 0.8f, 0.2f, 0.5f}});
		buffer->SafeAppend({pos1                    , {0.2f, 0.8f, 0.2f, 0.5f}});


		pos1 = float3(bx, 0.0f, by + dy * (a    ));
		pos2 = float3(bx, 0.0f, by + dy * (a + 1));
		pos1.y = CGround::GetHeightAboveWater(pos1.x, pos1.z, false);
		pos2.y = CGround::GetHeightAboveWater(pos2.x, pos2.z, false);

		buffer->SafeAppend({pos1                    , {0.2f, 0.8f, 0.2f, 0.5f}});
		buffer->SafeAppend({pos2                    , {0.2f, 0.8f, 0.2f, 0.5f}});
		buffer->SafeAppend({pos2 + UpVector * 100.0f, {0.2f, 0.8f, 0.2f, 0.5f}});

		buffer->SafeAppend({pos2 + UpVector * 100.0f, {0.2f, 0.8f, 0.2f, 0.5f}});
		buffer->SafeAppend({pos1 + UpVector * 100.0f, {0.2f, 0.8f, 0.2f, 0.5f}});
		buffer->SafeAppend({pos1                    , {0.2f, 0.8f, 0.2f, 0.5f}});


		pos1 = float3(bx + dx * 10.0f, 0.0f, by + dy * (a    ));
		pos2 = float3(bx + dx * 10.0f, 0.0f, by + dy * (a + 1));
		pos1.y = CGround::GetHeightAboveWater(pos1.x, pos1.z, false);
		pos2.y = CGround::GetHeightAboveWater(pos2.x, pos2.z, false);

		buffer->SafeAppend({pos1                    , {0.2f, 0.8f, 0.2f, 0.5f}});
		buffer->SafeAppend({pos2                    , {0.2f, 0.8f, 0.2f, 0.5f}});
		buffer->SafeAppend({pos2 + UpVector * 100.0f, {0.2f, 0.8f, 0.2f, 0.5f}});

		buffer->SafeAppend({pos2 + UpVector * 100.0f, {0.2f, 0.8f, 0.2f, 0.5f}});
		buffer->SafeAppend({pos1 + UpVector * 100.0f, {0.2f, 0.8f, 0.2f, 0.5f}});
		buffer->SafeAppend({pos1                    , {0.2f, 0.8f, 0.2f, 0.5f}});
	}

	shader->Enable();
	shader->SetUniformMatrix4x4<float>("u_movi_mat", false, CMatrix44f::Identity());
	shader->SetUniformMatrix4x4<float>("u_proj_mat", false, CMatrix44f::ClipOrthoProj01(globalRendering->supportClipSpaceControl * 1.0f));
	buffer->Submit(GL_TRIANGLES);
	shader->Disable();

	glAttribStatePtr->DisableDepthTest();
}
#endif


void CStartPosSelecter::Draw()
{
	if (gu->spectating) {
		delete this;
		return;
	}


	GL::RenderDataBufferC* buffer = GL::GetRenderBufferC();
	Shader::IProgramObject* shader = buffer->GetShader();

	// lua-fied!
	// DrawStartBox(buffer, shader);

	if (!showReadyBox)
		return;

	const float mx =                               float(mouse->lastx)  * globalRendering->pixelX;
	const float my = (globalRendering->viewSizeY - float(mouse->lasty)) * globalRendering->pixelY;


	glAttribStatePtr->EnableBlendMask();

	{
		gleDrawQuadC(readyBox, InBox(mx, my, readyBox)? SColor{0.7f, 0.2f, 0.2f, guiAlpha}: SColor{0.7f, 0.7f, 0.2f, guiAlpha}, buffer);

		glAttribStatePtr->BlendFunc(GL_SRC_ALPHA, GL_ONE);
		glAttribStatePtr->PolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	}

	{
		gleDrawQuadC(readyBox, InBox(mx, my, readyBox)? SColor{0.7f, 0.2f, 0.2f, guiAlpha}: SColor{0.7f, 0.7f, 0.2f, guiAlpha}, buffer);

		glAttribStatePtr->PolygonMode(GL_FRONT_AND_BACK, GL_FILL);
		glAttribStatePtr->BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	}
	{
		shader->Enable();
		shader->SetUniformMatrix4x4<float>("u_movi_mat", false, CMatrix44f::Identity());
		shader->SetUniformMatrix4x4<float>("u_proj_mat", false, CMatrix44f::ClipOrthoProj01(globalRendering->supportClipSpaceControl * 1.0f));
		buffer->Submit(GL_TRIANGLES);
		shader->Disable();
	}

	{
		// fit text into box
		const float ySize = readyBox.y2 - readyBox.y1;
		const float xSize = readyBox.x2 - readyBox.x1;

		const float yPos = 0.5f * (readyBox.y1 + readyBox.y2);
		const float xPos = 0.5f * (readyBox.x1 + readyBox.x2);

		const float unitWidth  = font->GetSize() * font->GetTextWidth("Ready") * globalRendering->pixelX;
		const float unitHeight = font->GetSize() * font->GetLineHeight(      ) * globalRendering->pixelY;

		const float fontScale = 0.9f * std::min(xSize / unitWidth, ySize / unitHeight);

		font->SetColors(); // default
		font->glPrint(xPos, yPos, fontScale, FONT_OUTLINE | FONT_CENTER | FONT_VCENTER | FONT_SCALE | FONT_NORM | FONT_BUFFERED, "Ready");
		font->DrawBufferedGL4();
	}
}