summaryrefslogtreecommitdiff
path: root/sourcemod/scripting/gokz-core/map/buttons.sp
blob: 8923fbd0c0fcb42ea05bf98b60c5c47eda7a67fd (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
/*
	Hooks between specifically named func_buttons and GOKZ.
*/



static Regex RE_BonusStartButton;
static Regex RE_BonusEndButton;



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

void OnPluginStart_MapButtons()
{
	RE_BonusStartButton = CompileRegex(GOKZ_BONUS_START_BUTTON_NAME_REGEX);
	RE_BonusEndButton = CompileRegex(GOKZ_BONUS_END_BUTTON_NAME_REGEX);
}

void OnEntitySpawned_MapButtons(int entity)
{
	char buffer[32];
	
	GetEntityClassname(entity, buffer, sizeof(buffer));
	if (!StrEqual("func_button", buffer, false))
	{
		return;
	}
	
	if (GetEntityName(entity, buffer, sizeof(buffer)) == 0)
	{
		return;
	}
	
	int course = 0;
	if (StrEqual(GOKZ_START_BUTTON_NAME, buffer, false))
	{
		HookSingleEntityOutput(entity, "OnPressed", OnStartButtonPress);
		RegisterCourseStart(course);
	}
	else if (StrEqual(GOKZ_END_BUTTON_NAME, buffer, false))
	{
		HookSingleEntityOutput(entity, "OnPressed", OnEndButtonPress);
		RegisterCourseEnd(course);
	}
	else if ((course = GetStartButtonBonusNumber(entity)) != -1)
	{
		HookSingleEntityOutput(entity, "OnPressed", OnBonusStartButtonPress);
		RegisterCourseStart(course);
	}
	else if ((course = GetEndButtonBonusNumber(entity)) != -1)
	{
		HookSingleEntityOutput(entity, "OnPressed", OnBonusEndButtonPress);
		RegisterCourseEnd(course);
	}
}

public void OnStartButtonPress(const char[] name, int caller, int activator, float delay)
{
	if (!IsValidEntity(caller) || !IsValidClient(activator))
	{
		return;
	}
	
	ProcessStartButtonPress(activator, 0);
}

public void OnEndButtonPress(const char[] name, int caller, int activator, float delay)
{
	if (!IsValidEntity(caller) || !IsValidClient(activator))
	{
		return;
	}
	
	ProcessEndButtonPress(activator, 0);
}

public void OnBonusStartButtonPress(const char[] name, int caller, int activator, float delay)
{
	if (!IsValidEntity(caller) || !IsValidClient(activator))
	{
		return;
	}
	
	int course = GetStartButtonBonusNumber(caller);
	if (!GOKZ_IsValidCourse(course, true))
	{
		return;
	}
	
	ProcessStartButtonPress(activator, course);
}

public void OnBonusEndButtonPress(const char[] name, int caller, int activator, float delay)
{
	if (!IsValidEntity(caller) || !IsValidClient(activator))
	{
		return;
	}
	
	int course = GetEndButtonBonusNumber(caller);
	if (!GOKZ_IsValidCourse(course, true))
	{
		return;
	}
	
	ProcessEndButtonPress(activator, course);
}



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

static void ProcessStartButtonPress(int client, int course)
{
	if (GOKZ_StartTimer(client, course))
	{
		// Only calling on success is intended behaviour (and prevents virtual button exploits)
		OnStartButtonPress_Teleports(client, course);
		OnStartButtonPress_VirtualButtons(client, course);
	}
}

static void ProcessEndButtonPress(int client, int course)
{
	GOKZ_EndTimer(client, course);
	OnEndButtonPress_VirtualButtons(client, course);
}

static int GetStartButtonBonusNumber(int entity)
{
	return GOKZ_MatchIntFromEntityName(entity, RE_BonusStartButton, 1);
}

static int GetEndButtonBonusNumber(int entity)
{
	return GOKZ_MatchIntFromEntityName(entity, RE_BonusEndButton, 1);
}