-
Notifications
You must be signed in to change notification settings - Fork 6
/
teleport_player.sp
282 lines (230 loc) · 8.08 KB
/
teleport_player.sp
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <sdktools>
#include <builtinvotes>
#define TEAM_SPECTATOR 1
enum VoteType
{
VoteType_GoTo,
VoteType_Bring
}
Handle hVote = INVALID_HANDLE;
VoteType g_VoteType;
int g_SourceClient = -1;
char g_Name[32];
float g_fTeleportDestination[3];
public Plugin myinfo =
{
name = "Teleport Player",
author = "devilesk",
description = "Adds sm_goto and sm_bring to teleport players.",
version = "0.1.0",
url = "https://github.com/devilesk/rl4d2l-plugins"
}
public void OnPluginStart()
{
RegConsoleCmd("sm_goto", Command_GoTo, "Go to a player.");
RegConsoleCmd("sm_bring", Command_Bring," Teleport a player to the point you are looking at.");
}
public Action Command_GoTo(int client, int args)
{
if(args < 1)
{
PrintToConsole(client, "Usage: sm_goto <name>");
PrintToChat(client, "Usage:\x04 sm_goto <name>");
return Plugin_Handled;
}
char sNameArg[32];
GetCmdArg(1, sNameArg, sizeof(sNameArg));
int target = -1;
for(int i = 1; i <= MaxClients; i++) {
if(!IsClientConnected(i)) continue;
GetClientName(i, g_Name, sizeof(g_Name));
if (StrEqual(g_Name, sNameArg, false)) {
target = i;
break;
}
else if (StrContains(g_Name, sNameArg, false) != -1) {
target = i;
}
}
if(target == -1)
{
PrintToConsole(client, "Could not find client \x04%s", sNameArg);
PrintToChat(client, "Could not find client \x04%s", sNameArg);
return Plugin_Handled;
}
GetClientName(target, g_Name, sizeof(g_Name));
bool bIsAdmin = CheckCommandAccess(client, "sm_goto", ADMFLAG_KICK, true);
if (!bIsAdmin) {
if (IsSpectator(client)) {
PrintToChat(client, "\x01[\x04Teleport Player\x01] Vote can only be started by a player!");
return Plugin_Handled;
}
else if (!IsNewBuiltinVoteAllowed()) {
PrintToChat(client, "\x01[\x04Teleport Player\x01] Vote cannot be started now.");
return Plugin_Handled;
}
}
else {
if (IsBuiltinVoteInProgress()) {
PrintToChat(client, "\x01[\x04Teleport Player\x01] There is a vote in progress.");
return Plugin_Handled;
}
}
g_SourceClient = client;
float fTargetOrigin[3];
GetClientAbsOrigin(target, fTargetOrigin);
g_fTeleportDestination[0] = fTargetOrigin[0];
g_fTeleportDestination[1] = fTargetOrigin[1];
g_fTeleportDestination[2] = (fTargetOrigin[2] + 73);
if (bIsAdmin) {
DoTeleport();
}
else {
char prompt[100];
Format(prompt, sizeof(prompt), "Allow %N to go to %s?", client, g_Name);
StartVote(client, prompt, VoteType_GoTo);
FakeClientCommand(client, "Vote Yes");
}
return Plugin_Handled;
}
public Action Command_Bring(int client, int args)
{
if(args < 1)
{
PrintToConsole(client, "Usage: sm_bring <name>");
PrintToChat(client, "Usage:\x04 sm_bring <name>");
return Plugin_Handled;
}
char sNameArg[32];
GetCmdArg(1, sNameArg, sizeof(sNameArg));
int target = -1;
for(int i = 1; i <= MaxClients; i++) {
if(!IsClientConnected(i)) continue;
GetClientName(i, g_Name, sizeof(g_Name));
if (StrEqual(g_Name, sNameArg, false)) {
target = i;
break;
}
else if (StrContains(g_Name, sNameArg, false) != -1) {
target = i;
}
}
if(target == -1)
{
PrintToConsole(client, "Could not find client \x04%s", sNameArg);
PrintToChat(client, "Could not find client \x04%s", sNameArg);
return Plugin_Handled;
}
GetClientName(target, g_Name, sizeof(g_Name));
bool bIsAdmin = CheckCommandAccess(client, "sm_bring", ADMFLAG_KICK, true);
if (!bIsAdmin) {
if (IsSpectator(client)) {
PrintToChat(client, "\x01[\x04Teleport Player\x01] Vote can only be started by a player!");
return Plugin_Handled;
}
else if (!IsNewBuiltinVoteAllowed()) {
PrintToChat(client, "\x01[\x04Teleport Player\x01] Vote cannot be started now.");
return Plugin_Handled;
}
}
else {
if (IsBuiltinVoteInProgress()) {
PrintToChat(client, "\x01[\x04Teleport Player\x01] There is a vote in progress.");
return Plugin_Handled;
}
}
g_SourceClient = target;
float fTargetOrigin[3];
GetCollisionPoint(client, fTargetOrigin);
g_fTeleportDestination[0] = fTargetOrigin[0];
g_fTeleportDestination[1] = fTargetOrigin[1];
g_fTeleportDestination[2] = (fTargetOrigin[2] + 4);
if (bIsAdmin) {
DoTeleport();
}
else {
char prompt[100];
Format(prompt, sizeof(prompt), "Allow %s to go to %N?", g_Name, client);
StartVote(client, prompt, VoteType_Bring);
FakeClientCommand(client, "Vote Yes");
}
return Plugin_Handled;
}
public void StartVote(int client, const char[] sVoteHeader, VoteType voteType) {
int iNumPlayers;
int[] players = new int[MaxClients];
for (int i = 1; i <= MaxClients; i++) {
if (!IsClientConnected(i) || !IsClientInGame(i)) continue;
if (IsSpectator(i) || IsFakeClient(i)) continue;
players[iNumPlayers++] = i;
}
g_VoteType = voteType;
hVote = CreateBuiltinVote(VoteActionHandler, BuiltinVoteType_Custom_YesNo, BuiltinVoteAction_Cancel | BuiltinVoteAction_VoteEnd | BuiltinVoteAction_End);
SetBuiltinVoteArgument(hVote, sVoteHeader);
SetBuiltinVoteInitiator(hVote, client);
SetBuiltinVoteResultCallback(hVote, VoteResultHandler);
DisplayBuiltinVote(hVote, players, iNumPlayers, 20);
}
public void VoteActionHandler(Handle vote, BuiltinVoteAction action, int param1, int param2) {
switch (action) {
case BuiltinVoteAction_End: {
hVote = INVALID_HANDLE;
delete vote;
}
case BuiltinVoteAction_Cancel: {
DisplayBuiltinVoteFail(vote, view_as<BuiltinVoteFailReason>(param1));
}
}
}
public void VoteResultHandler(Handle vote, int num_votes, int num_clients, const int[][] client_info, int num_items, const int[][] item_info) {
for (int i = 0; i < num_items; i++) {
if (item_info[i][BUILTINVOTEINFO_ITEM_INDEX] == BUILTINVOTES_VOTE_YES) {
if (item_info[i][BUILTINVOTEINFO_ITEM_VOTES] > (num_clients / 2)) {
DisplayBuiltinVotePass(vote, "Teleporting player...");
PrintToChatAll("\x01[\x04Teleport Player\x01] Vote passed! Teleporting player...");
PrintDebug("[VoteResultHandler] Vote passed! Teleporting player...");
if (g_VoteType == VoteType_GoTo)
DoTeleport();
else if (g_VoteType == VoteType_Bring)
DoTeleport();
return;
}
}
}
DisplayBuiltinVoteFail(vote, BuiltinVoteFail_Loses);
}
void DoTeleport() {
PrintDebug("[DoTeleport] Teleporting %N to %f %f %f", g_SourceClient, g_fTeleportDestination[0], g_fTeleportDestination[1], g_fTeleportDestination[2]);
TeleportEntity(g_SourceClient, g_fTeleportDestination, NULL_VECTOR, NULL_VECTOR);
}
bool IsSpectator(int client) {
return client > 0 && client <= MaxClients && IsClientInGame(client) && GetClientTeam(client) == TEAM_SPECTATOR;
}
// Trace
stock void GetCollisionPoint(int client, float pos[3])
{
float vOrigin[3];
float vAngles[3];
GetClientEyePosition(client, vOrigin);
GetClientEyeAngles(client, vAngles);
Handle trace = TR_TraceRayFilterEx(vOrigin, vAngles, MASK_SOLID, RayType_Infinite, TraceEntityFilterPlayer);
if(TR_DidHit(trace))
{
TR_GetEndPosition(pos, trace);
delete trace;
return;
}
delete trace;
}
public bool TraceEntityFilterPlayer(int entity, int contentsMask)
{
return entity > MaxClients;
}
stock void PrintDebug(const char[] Message, any ...) {
char DebugBuff[256];
VFormat(DebugBuff, sizeof(DebugBuff), Message, 2);
LogMessage(DebugBuff);
}