summaryrefslogtreecommitdiff
path: root/sourcemod/scripting/include/gamechaos/misc.inc
blob: f964862fda739349ffda09e71eb2f09e65aef903 (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
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

#if defined _gamechaos_stocks_misc_included
	#endinput
#endif
#define _gamechaos_stocks_misc_included

#define GC_MISC_VERSION 0x01_00_00
#define GC_MISC_VERSION_STRING "1.0.0"

/**
 * Check if player is overlapping their MOVERIGHT and MOVELEFT buttons.
 *
 * @param x					Buttons;
 * @return 					True if overlapping, false otherwise.
 */
stock bool GCIsOverlapping(int buttons)
{
	return buttons & IN_MOVERIGHT && buttons & IN_MOVELEFT
}

/**
 * Checks if player gained speed.
 *
 * @param speed				Current player speed.
 * @param lastspeed			Player speed from previous tick.
 * @return 					True if player gained speed, false otherwise.
 */
stock bool GCIsStrafeSynced(float speed, float lastspeed)
{
	return speed > lastspeed;
}

/**
 * Checks if the player is not holding down their MOVERIGHT and MOVELEFT buttons.
 *
 * @param x					Buttons.
 * @return 					True if they're not holding either, false otherwise.
 */
stock bool GCIsDeadAirtime(int buttons)
{
	return !(buttons & IN_MOVERIGHT) && !(buttons & IN_MOVELEFT);
}

/** 
* Source: https://forums.alliedmods.net/showthread.php?p=2535972
* Runs a single line of vscript code. 
* NOTE: Dont use the "script" console command, it startes a new instance and leaks memory. Use this instead! 
* 
* @param code				The code to run. 
* @noreturn 
*/ 
stock void GCRunScriptCode(const char[] code, any ...) 
{ 
	static int scriptLogic = INVALID_ENT_REFERENCE; 
	
	if (scriptLogic == INVALID_ENT_REFERENCE || !IsValidEntity(scriptLogic))
	{
		scriptLogic = EntIndexToEntRef(CreateEntityByName("logic_script")); 
		if (scriptLogic == INVALID_ENT_REFERENCE || !IsValidEntity(scriptLogic))
		{
			SetFailState("Could not create a 'logic_script' entity."); 
		}
		
		DispatchSpawn(scriptLogic); 
	} 
	
	char buffer[512]; 
	VFormat(buffer, sizeof(buffer), code, 2); 
	
	SetVariantString(buffer); 
	AcceptEntityInput(scriptLogic, "RunScriptCode"); 
}

stock void GCTE_SendBeamBox(int client,
						  const float origin[3],
						  const float mins[3],
						  const float maxs[3],
						  int ModelIndex,
						  int HaloIndex = 0,
						  float Life = 3.0,
						  float Width = 2.0,
						  const int Colour[4] =  { 255, 255, 255, 255 },
						  float EndWidth = 2.0,
						  int StartFrame = 0,
						  int FrameRate = 0,
						  int FadeLength = 0,
						  float Amplitude = 0.0,
						  int Speed = 0)
{
	// credit to some bhop timer by shavit? thanks
	int pairs[8][3] =  { { 0, 0, 0 }, { 1, 0, 0 }, { 1, 1, 0 }, { 0, 1, 0 }, { 0, 0, 1 }, { 1, 0, 1 }, { 1, 1, 1 }, { 0, 1, 1 } };
	int edges[12][2] =  { { 0, 1 }, { 0, 3 }, { 0, 4 }, { 2, 1 }, { 2, 3 }, { 2, 6 }, { 5, 4 }, { 5, 6 }, { 5, 1 }, { 7, 4 }, { 7, 6 }, { 7, 3 } };
	
	float corners[8][3];
	float corner[2][3];
	
	AddVectors(origin, mins, corner[0]);
	AddVectors(origin, maxs, corner[1]);
	
	for (int i = 0; i < 8; i++)
	{
		corners[i][0] = corner[pairs[i][0]][0];
		corners[i][1] = corner[pairs[i][1]][1];
		corners[i][2] = corner[pairs[i][2]][2];
	}
	
	for (int i = 0; i < 12; i++)
	{
		TE_SetupBeamPoints(corners[edges[i][0]],
						   corners[edges[i][1]],
						   ModelIndex,
						   HaloIndex,
						   StartFrame,
						   FrameRate,
						   Life,
						   Width,
						   EndWidth,
						   FadeLength,
						   Amplitude,
						   Colour,
						   Speed);
		TE_SendToClient(client);
	}
}

stock void GCTE_SendBeamCross(int client,
							const float origin[3],
							int ModelIndex,
							int HaloIndex = 0,
							float Life = 3.0,
							float Width = 2.0,
							const int Colour[4]  = { 255, 255, 255, 255 },
							float EndWidth = 2.0,
							int StartFrame = 0,
							int FrameRate = 0,
							int FadeLength = 0,
							float Amplitude = 0.0,
							int Speed = 0)
{
	float points[4][3];
	
	for (int i; i < 4; i++)
	{
		points[i][2] = origin[2];
	}
	
	// -x; -y
	points[0][0] = origin[0] - 8.0;
	points[0][1] = origin[1] - 8.0;
	
	// +x; -y
	points[1][0] = origin[0] + 8.0;
	points[1][1] = origin[1] - 8.0;
	
	// +x; +y
	points[2][0] = origin[0] + 8.0;
	points[2][1] = origin[1] + 8.0;
	
	// -x; +y
	points[3][0] = origin[0] - 8.0;
	points[3][1] = origin[1] + 8.0;
	
	//draw cross
	for (int corner; corner < 4; corner++)
	{
		TE_SetupBeamPoints(origin, points[corner], ModelIndex, HaloIndex, StartFrame, FrameRate, Life, Width, EndWidth, FadeLength, Amplitude, Colour, Speed);
		TE_SendToClient(client);
	}
}

stock void GCTE_SendBeamRectangle(int client,
						  const float origin[3],
						  const float mins[3],
						  const float maxs[3],
						  int modelIndex,
						  int haloIndex = 0,
						  float life = 3.0,
						  float width = 2.0,
						  const int colour[4]  = { 255, 255, 255, 255 },
						  float endWidth = 2.0,
						  int startFrame = 0,
						  int frameRate = 0,
						  int fadeLength = 0,
						  float amplitude = 0.0,
						  int speed = 0)
{
	float vertices[4][3];
	GCRectangleVerticesFromPoint(vertices, origin, mins, maxs);
	
	// send the square
	for (int i; i < 4; i++)
	{
		int j = (i == 3) ? (0) : (i + 1);
		TE_SetupBeamPoints(vertices[i],
						   vertices[j],
						   modelIndex,
						   haloIndex,
						   startFrame,
						   frameRate,
						   life,
						   width,
						   endWidth,
						   fadeLength,
						   amplitude,
						   colour,
						   speed);
		TE_SendToClient(client);
	}
}

/**
 * Calculates vertices for a rectangle from a point, mins and maxs.
 *
 * @param result			Vertex array result.
 * @param origin			Origin to offset mins and maxs by.
 * @param mins				Minimum size of the rectangle.
 * @param maxs				Maximum size of the rectangle.
 * @return 					True if overlapping, false otherwise.
 */
stock void GCRectangleVerticesFromPoint(float result[4][3], const float origin[3], const float mins[3], const float maxs[3])
{
	// Vertices are set clockwise starting from top left (-x; -y)
	
	// -x; -y
	result[0][0] = origin[0] + mins[0];
	result[0][1] = origin[1] + mins[1];
	
	// +x; -y
	result[1][0] = origin[0] + maxs[0];
	result[1][1] = origin[1] + mins[1];
	
	// +x; +y
	result[2][0] = origin[0] + maxs[0];
	result[2][1] = origin[1] + maxs[1];
	
	// -x; +y
	result[3][0] = origin[0] + mins[0];
	result[3][1] = origin[1] + maxs[1];
	
	// z is the same for every vertex
	for (int vertex; vertex < 4; vertex++)
	{
		result[vertex][2] = origin[2];
	}
}