-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpectate.lsl
211 lines (205 loc) · 7.13 KB
/
Spectate.lsl
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
/*
Spectate script written by Thunder Rahja.
This work is licensed under GNU GPLv3, which should be included with this script.
Instructions: Drop this script into an object, preferably an attachment. Spectate a target using the command
"spec [name]" where [name] is a partial name of the target. Camera views can be changed with these commands:
auto, shoulder, chase, nose. Camera offset can be changed with the command "offset [ratio]" which uses a default
ratio of 1. To stop spectating, press any movement key (while not sitting) or use the command "stop" to return
your camera to your avatar. When you first specify a target, you may have have to reset your camera (ESC) to begin
spectating. This script listens on channel 13, which can be changed below.
*/
// Configuration
integer USER_CHANNEL = 13; // User channel
// Internal
integer cameraMode; // 0 = auto, 1 = shoulder, 2 = chase, 3 = nose
float camOffset = 1;
float refreshRate = 0.022222; // 45 FPS, the maximum possible under ideal conditions
key targetKey;
vector scaleOffset;
integer myPerms;
list FindNamesLike(string namePart) // returns strided list of names and UUIDs
{
namePart = llDumpList2String(llParseString2List(llToLower(namePart), ["."], []), " ");
list output;
list agentList = llGetAgentList(AGENT_LIST_REGION, []);
integer n = llListFindList(agentList, [llGetOwner()]);
agentList = llDeleteSubList(agentList, n, n); // remove owner from list
n = llGetListLength(agentList);
while (n--)
{
key agentKey = llList2Key(agentList, n);
string agentName = llKey2Name(agentKey);
if (llSubStringIndex(llToLower(agentName), namePart) == 0)
{
if (llGetSubString(agentName, -9, -1) == " Resident")
agentName = llDeleteSubString(agentName, -9, -1);
if (llToLower(agentName) == namePart) // exact match found
{
return [agentName, agentKey];
}
else output += [agentName, agentKey];
}
}
return llListSort(output, 2, TRUE);
}
Stop()
{
llSetTimerEvent(0);
targetKey = "";
if (myPerms & PERMISSION_CONTROL_CAMERA) llClearCameraParams();
llReleaseControls();
}
default
{
state_entry()
{
if (llGetAttached()) llRequestPermissions(llGetOwner(), PERMISSION_CONTROL_CAMERA);
llListen(USER_CHANNEL, "", llGetOwner(), "");
}
changed(integer change)
{
if (change & CHANGED_OWNER)
{
llResetScript();
}
else if (change & CHANGED_TELEPORT)
{
Stop();
}
}
attach(key id)
{
if (id) Stop();
}
run_time_permissions(integer perm)
{
myPerms = perm;
if (perm & PERMISSION_TAKE_CONTROLS)
{
llTakeControls(831, TRUE, TRUE); // All movement controls
}
if (perm & PERMISSION_CONTROL_CAMERA)
{
if (targetKey)
{
llSetTimerEvent(refreshRate);
}
else
{
llClearCameraParams();
}
}
}
listen(integer channel, string name, key id, string text)
{
list params = llParseString2List(text, [" "], []);
string command = llToLower(llList2String(params, 0));
if (command == "spec")
{
string targetName = llDumpList2String(llDeleteSubList(params, 0, 0), " ");
list matches = FindNamesLike(targetName);
integer matchCount = llGetListLength(matches);
if (matchCount > 2)
{
llOwnerSay("Clarify target: " + llDumpList2String(llList2ListStrided(matches, 0, -1, 2), " | "));
}
else if (matchCount == 0)
{
llOwnerSay("Target not found: " + targetName);
}
else
{
targetKey = llList2Key(matches, 1);
vector targetSize = llGetAgentSize(targetKey);
scaleOffset = <0, 0, targetSize.z * 0.48>;
llRequestPermissions(llGetOwner(), PERMISSION_TAKE_CONTROLS | PERMISSION_CONTROL_CAMERA);
}
}
else if (command == "stop")
{
Stop();
}
else if (command == "auto")
{
cameraMode = 0;
}
else if (command == "shoulder")
{
cameraMode = 1;
}
else if (command == "chase")
{
cameraMode = 2;
}
else if (command == "nose")
{
cameraMode = 3;
}
else if (command == "offset")
{
float newOffset = (float)llDeleteSubString(text, 0, 6);
if (newOffset > 0)
{
camOffset = newOffset;
llOwnerSay("Camera offset ratio: " + (string)newOffset);
}
}
else if (command == "rate")
{
integer newRate = llList2Integer(params, 1);
if (newRate > 45) newRate = 45;
if (newRate > 0)
{
refreshRate = 1.0 / newRate;
if (targetKey) llSetTimerEvent(refreshRate);
llOwnerSay("Camera update rate: " + (string)newRate + " FPS.");
}
}
}
control(key id, integer level, integer edge)
{
Stop();
}
timer()
{
if (targetKey)
{
if (myPerms & PERMISSION_CONTROL_CAMERA)
{
list targetInfo = llGetObjectDetails(targetKey, [OBJECT_POS, OBJECT_ROT]);
vector targetPos = llList2Vector(targetInfo, 0);
if (targetPos)
{
vector newCameraPos;
rotation targetRot = llList2Rot(targetInfo, 1);
integer effectiveMode = cameraMode;
if (cameraMode == 0) // auto
{
if (llGetAgentInfo(targetKey) & AGENT_MOUSELOOK) effectiveMode = 3;
else effectiveMode = 1;
}
if (effectiveMode == 1) // shoulder
newCameraPos = targetPos + scaleOffset +
<-camOffset * 1.5, -camOffset * 0.5, camOffset * 0.25> * targetRot;
else if (effectiveMode == 2) // chase
newCameraPos = targetPos +
<-camOffset * 3, 0, camOffset> * targetRot;
else if (effectiveMode == 3) // nose
newCameraPos = targetPos + scaleOffset +
<camOffset, 0, 0> * targetRot;
vector newCameraFocus = newCameraPos + <5,0,0> * targetRot;
llSetCameraParams([CAMERA_ACTIVE, TRUE, CAMERA_FOCUS, newCameraFocus, CAMERA_POSITION, newCameraPos,
CAMERA_POSITION_LOCKED, TRUE, CAMERA_FOCUS_LOCKED, TRUE]);
}
else
{
if (llGetAgentSize(targetKey) == ZERO_VECTOR)
{
llOwnerSay("Target lost.");
Stop();
}
}
}
}
}
}