summaryrefslogtreecommitdiff
path: root/sourcemod/scripting/gokz-hud/racing_text.sp
blob: ac6ea3d387ab0dfcb600444eaed99054ac1988d8 (plain)
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
/*	
	Uses HUD text to show the race countdown and a start message.

	This is manually refreshed when a race starts to show the start message	as
	soon as possible, improving responsiveness.
*/



static Handle racingHudSynchronizer;
static float countdownStartTime[MAXPLAYERS + 1];



// =====[ EVENTS ]=====

void OnPluginStart_RacingText()
{
	racingHudSynchronizer = CreateHudSynchronizer();
}

void OnPlayerRunCmdPost_RacingText(int client, int cmdnum)
{
	int updateSpeed = gB_FastUpdateRate[client] ? 3 : 6;
	if (gB_GOKZRacing && cmdnum % updateSpeed == 2)
	{
		UpdateRacingText(client);
	}
}

void OnRaceInfoChanged_RacingText(int raceID, RaceInfo prop, int newValue)
{
	if (prop != RaceInfo_Status)
	{
		return;
	}

	if (newValue == RaceStatus_Countdown)
	{
		for (int client = 1; client <= MaxClients; client++)
		{
			if (GOKZ_RC_GetRaceID(client) == raceID)
			{
				countdownStartTime[client] = GetGameTime();
			}
		}
	}
	else if (newValue == RaceStatus_Aborting)
	{
		for (int client = 1; client <= MaxClients; client++)
		{
			if (GOKZ_RC_GetRaceID(client) == raceID)
			{
				ClearRacingText(client);
			}
		}
	}
	else if (newValue == RaceStatus_Started)
	{
		for (int client = 1; client <= MaxClients; client++)
		{
			if (GOKZ_RC_GetRaceID(client) == raceID)
			{
				UpdateRacingText(client);
			}
		}
	}
}



// =====[ PRIVATE ]=====

static void UpdateRacingText(int client)
{
	KZPlayer player = KZPlayer(client);

	if (player.Fake)
	{
		return;
	}

	if (player.Alive)
	{
		ShowRacingText(player, player);
	}
	else
	{
		KZPlayer targetPlayer = KZPlayer(player.ObserverTarget);
		if (targetPlayer.ID != -1 && !targetPlayer.Fake)
		{
			ShowRacingText(player, targetPlayer);
		}
	}
}

static void ClearRacingText(int client)
{
	ClearSyncHud(client, racingHudSynchronizer);
}

static void ShowRacingText(KZPlayer player, KZPlayer targetPlayer)
{
	if (GOKZ_RC_GetStatus(targetPlayer.ID) != RacerStatus_Racing)
	{
		return;
	}

	int raceStatus = GOKZ_RC_GetRaceInfo(GOKZ_RC_GetRaceID(targetPlayer.ID), RaceInfo_Status);
	if (raceStatus == RaceStatus_Countdown)
	{
		ShowCountdownText(player, targetPlayer);
	}
	else if (raceStatus == RaceStatus_Started)
	{
		ShowStartedText(player, targetPlayer);
	}
}

static void ShowCountdownText(KZPlayer player, KZPlayer targetPlayer)
{
	float timeToStart = (countdownStartTime[targetPlayer.ID] + RC_COUNTDOWN_TIME) - GetGameTime();
	int colour[4];
	GetCountdownColour(timeToStart, colour);

	SetHudTextParams(-1.0, 0.3, GetTextHoldTime(gB_FastUpdateRate[player.ID] ? 3 : 6), colour[0], colour[1], colour[2], colour[3], 0, 1.0, 0.0, 0.0);
	ShowSyncHudText(player.ID, racingHudSynchronizer, "%t\n\n%d", "Get Ready", IntMax(RoundToCeil(timeToStart), 1));
}

static void GetCountdownColour(float timeToStart, int buffer[4])
{
	// From red to green
	if (timeToStart >= RC_COUNTDOWN_TIME)
	{
		buffer[0] = 255;
		buffer[1] = 0;
	}
	else if (timeToStart > RC_COUNTDOWN_TIME / 2.0)
	{
		buffer[0] = 255;
		buffer[1] = RoundFloat(-510.0 / RC_COUNTDOWN_TIME * timeToStart + 510.0);
	}
	else if (timeToStart > 0.0)
	{
		buffer[0] = RoundFloat(510.0 / RC_COUNTDOWN_TIME * timeToStart);
		buffer[1] = 255;
	}
	else
	{
		buffer[0] = 0;
		buffer[1] = 255;
	}

	buffer[2] = 0;
	buffer[3] = 255;
}

static void ShowStartedText(KZPlayer player, KZPlayer targetPlayer)
{
	if (targetPlayer.TimerRunning)
	{
		return;
	}

	SetHudTextParams(-1.0, 0.3, GetTextHoldTime(gB_FastUpdateRate[player.ID] ? 3 : 6), 0, 255, 0, 255, 0, 1.0, 0.0, 0.0);
	ShowSyncHudText(player.ID, racingHudSynchronizer, "%t", "Go!");
}