-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathl4d2_draw_avoid.sp
More file actions
150 lines (129 loc) · 3.38 KB
/
Copy pathl4d2_draw_avoid.sp
File metadata and controls
150 lines (129 loc) · 3.38 KB
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
#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <sdktools>
#include <profiler>
enum struct Circle
{
float pos[3];
float width;
}
#define MAX_OBJ 20
int g_sprite;
int g_halo;
Circle circle[MAX_OBJ];
Handle g_timer;
public void OnPluginStart()
{
RegAdminCmd("sm_draw_avoid", CommandDrawAvoid, ADMFLAG_ROOT);
RegAdminCmd("sm_benchmark_avoid", CommandBenchmarkAvoid, ADMFLAG_ROOT);
}
public void OnMapStart()
{
g_timer = null;
g_sprite = PrecacheModel("materials/sprites/laserbeam.vmt", true);
g_halo = PrecacheModel("materials/dev/halo_add_to_screen.vmt", true);
}
Action CommandDrawAvoid(int client, int args)
{
float origin[3];
GetClientAbsOrigin(client, origin);
origin[2] += 5.0;
SetupCircles(origin);
delete g_timer;
g_timer = CreateTimer(0.1, TimerDrawStep, _, TIMER_REPEAT | TIMER_FLAG_NO_MAPCHANGE);
return Plugin_Handled;
}
Action CommandBenchmarkAvoid(int client, int args)
{
PrintToServer("SM INC VERSION %s", SOURCEMOD_VERSION);
SetupCircles();
Handle hProf = CreateProfiler();
float min = 10.0, max, avg;
int round = 20;
// Running the test 20 times to gain min/max/avg
for (int x = 0; x < round; x++)
{
StartProfiling(hProf);
// Running 1000 iterations for our test
for (int i = 0; i < 1000; i++)
{
StepAvoidAll(.benchmark = true);
}
StopProfiling(hProf);
float speed = GetProfilerTime(hProf);
if (speed < min) min = speed;
if (speed > max) max = speed;
avg += speed;
}
// Display our minimum, maximum and average times.
avg /= float(round);
PrintToServer("Bench (Batch 1000): Min %.2fms. Avg %.2fms. Max %.2fms", min * 1000.0, avg * 1000.0, max * 1000.0);
delete hProf;
return Plugin_Handled;
}
void SetupCircles(const float origin[3] = { 0.0, 0.0, 0.0 })
{
for (int i = 0; i < sizeof(circle); i++)
{
/*
circle[i].pos[0] = GetRandomFloat(-200.0, 200.0) + origin[0];
circle[i].pos[1] = GetRandomFloat(-200.0, 200.0) + origin[1];
circle[i].pos[2] = origin[2];
circle[i].width = GetRandomFloat(20.0, 100.0);*/
circle[i].pos[0] = origin[0] + i;
circle[i].pos[1] = origin[1] + i;
circle[i].pos[2] = origin[2];
circle[i].width = (i + 1) * 2.0;
}
}
Action TimerDrawStep(Handle timer)
{
StepAvoidAll(.benchmark = false);
return Plugin_Continue;
}
void StepAvoidAll(bool benchmark)
{
for (int i = 0; i < sizeof(circle); i++)
{
StepAvoidOne(i, benchmark);
}
}
void StepAvoidOne(int idx, bool benchmark)
{
float toThem[3],pos[3],avoid[3],avoidWeight;
pos = circle[idx].pos;
float x = pos[0];
float y = pos[1];
float my_width = circle[idx].width;
for (int i = 0; i < sizeof(circle); i++)
{
if (i == idx) continue;
float other_width = circle[i].width;
float threshold = (my_width + other_width) * 0.5;
toThem[0] = circle[i].pos[0] - x;
toThem[1] = circle[i].pos[1] - y;
toThem[2] = 0.0;
float range = NormalizeVector(toThem, toThem);
if (range < threshold)
{
float penetration = threshold - range;
float weight = 1.0 + (2.0 * penetration / threshold);
avoid[0] -= weight * toThem[0];
avoid[1] -= weight * toThem[1];
avoidWeight += weight;
}
}
if (avoidWeight > 0.0)
{
float len = 5.0 / avoidWeight;
pos[0] += avoid[0] * len;
pos[1] += avoid[1] * len;
}
if (!benchmark)
{
circle[idx].pos = pos;
TE_SetupBeamRingPoint(pos, my_width, my_width + 0.1, g_sprite, g_halo, 0, 0, 0.1, 2.0, 0.0, { 255, 255, 0, 200 }, 0, 0);
TE_SendToAll();
}
}