-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_manager.lsl
536 lines (482 loc) · 19.8 KB
/
node_manager.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#include "shared.inc.lsl"
#define MENU_CHANNEL_BASE -21461421
#define CONNECTION_MENU_CHANNEL_BASE -21461422
#define PRUNE_NODES_EVERY 5
#define MAX_REZ_SLOTS 5
#define NODE_STRIDE 2
#define NODE_ID_OFFSET 0
#define NODE_KEY_OFFSET 1
// pending list, before we have a key
#define NODE_POS_OFFSET 1
// node_id, node_object_key
list gNodes = [];
// node_id, node_pos
list gPendingNodes = [];
// ORed together indices into gNodes list
list gNodeRelations = [];
// same as above, but relations that can't be actualized
// because we haven't rezzed the dst node yet.
list gPendingRelations = [];
list gArrowPool = [];
integer gRezNum;
integer gRestoreArrows;
integer gRestoring;
integer gNeededArrows;
integer gNeededNodes;
// arbitrary, under the message queue limit
// so we don't overwhelm the rez servers with messages,
// or overwhelm ourselves with responses to the rezzes.
integer gAvailRezSlots = MAX_REZ_SLOTS;
integer gLastRezTime;
integer gClickTimeout;
integer gTickCount;
string gFirstClicked = "";
string gSecondClicked = "";
// data manager doesn't know about some changes to the graph
integer gGraphDirty;
integer gMenuChannel;
integer gConnectionMenuChannel;
key nodeIDToKey(string node_id) {
integer idx = llListFindList(gNodes, [node_id]);
if (idx == -1)
return NULL_KEY;
return uncompressKey(llList2String(gNodes, idx + NODE_KEY_OFFSET));
}
string nodeKeyToID(key id) {
integer idx = llListFindList(gNodes, [compressKey(id)]);
if (idx == -1)
return "";
return llList2String(gNodes, idx - NODE_KEY_OFFSET);
}
dumpNodeDetails() {
integer i;
integer len = llGetListLength(gNodes);
llOwnerSay("gNodes:");
for(i=0; i<len; i+=NODE_STRIDE) {
llOwnerSay(llList2String(gNodes, i + NODE_ID_OFFSET) + ": "
+ (string)uncompressKey(llList2String(gNodes, i + NODE_KEY_OFFSET)));
}
len = llGetListLength(gPendingNodes);
llOwnerSay("gPendingNodes:");
for(i=0; i<len; i+=NODE_STRIDE) {
llOwnerSay(llList2String(gPendingNodes, i + NODE_ID_OFFSET) + ": "
+ llList2String(gPendingNodes, i + NODE_POS_OFFSET));
}
len = llGetListLength(gNodeRelations);
llOwnerSay("NODE RELATIONS:");
for(i=0; i<len; ++i) {
integer rel = llList2Integer(gNodeRelations, i);
llOwnerSay(llList2String(gNodes, UNPACK_REL_SRC(rel)) + ": "
+ llList2String(gNodes, UNPACK_REL_DST(rel)));
}
len = llGetListLength(gPendingRelations);
llOwnerSay("PENDING NODE RELATIONS:");
for(i=0; i<len; ++i) {
integer rel = llList2Integer(gPendingRelations, i);
llOwnerSay(llList2String(gNodes, UNPACK_REL_SRC(rel)) + ": "
+ llList2String(gNodes, UNPACK_REL_DST(rel)));
}
llOwnerSay("Free Memory: " + (string)llGetFreeMemory());
}
trackNode(string node_id, key node_key) {
gNodes += [node_id, compressKey(node_key)];
}
untrackNode(string node_id, integer kill_relations) {
integer node_idx = llListFindList(gNodes, [node_id]);
if (node_idx == -1)
return;
if (kill_relations) {
integer len = llGetListLength(gNodeRelations);
integer i;
// Backwards iteration so we can remove as we go without changing indices
for(i=len-1; i>=0; --i) {
integer rel = llList2Integer(gNodeRelations, i);
integer src_idx = UNPACK_REL_SRC(rel);
integer dst_idx = UNPACK_REL_DST(rel);
integer new_src_idx = src_idx;
integer new_dst_idx = dst_idx;
// removing this node will shift all indices down, may need to update
// the relation list to acommodate.
if (src_idx > node_idx)
new_src_idx -= NODE_STRIDE;
if (dst_idx > node_idx)
new_dst_idx -= NODE_STRIDE;
if (src_idx == node_idx || dst_idx == node_idx) {
// relation stemming from or pointing to the removed node,
// remove the relation. make sure to account for the removed
// node_idx when doing the lookup
string src_id = llList2String(gNodes, src_idx);
string dst_id = llList2String(gNodes, dst_idx);
llRegionSay(NODE_CHANNEL, "arrow_kill:" + src_id + ":" + dst_id);
gNodeRelations = llDeleteSubList(gNodeRelations, i, i);
} else if (new_src_idx != src_idx || new_dst_idx != dst_idx) {
integer new_rel = PACK_RELATION(new_src_idx, new_dst_idx);
gNodeRelations = llListReplaceList(gNodeRelations, [new_rel], i, i);
}
}
}
gNodes = llDeleteSubList(gNodes, node_idx, node_idx + NODE_STRIDE - 1);
}
pruneNodes() {
integer len = llGetListLength(gNodes);
integer i;
// disconnect nodes that are no longer represented in-world
for(i=len-NODE_STRIDE; i>=0; i-=NODE_STRIDE) {
// no mass == gone
key node_key = uncompressKey(llList2String(gNodes, i + NODE_KEY_OFFSET));
if (llGetObjectMass(node_key) == 0.0) {
untrackNode(llList2String(gNodes, i + NODE_ID_OFFSET), TRUE);
}
}
}
saveNodes() {
llMessageLinked(LINK_THIS, IPC_INIT_SAVE_INWORLD_STATE, "", NULL_KEY);
integer i;
integer len = llGetListLength(gNodes);
for(i=0; i<len; i+=NODE_STRIDE) {
string node_id = llList2String(gNodes, i + NODE_ID_OFFSET);
key node_key = uncompressKey(llList2String(gNodes, i + NODE_KEY_OFFSET));
vector node_pos = llList2Vector(llGetObjectDetails(node_key, [OBJECT_POS]), 0);
llMessageLinked(LINK_THIS, IPC_SAVE_NODE, node_id, (key)((string)node_pos));
// message queueing limits...
if ((i % 25) == 0)
llSleep(0.5);
}
len = llGetListLength(gNodeRelations);
for(i=0; i<len; ++i) {
integer rel = llList2Integer(gNodeRelations, i);
llMessageLinked(
LINK_THIS,
IPC_SAVE_RELATION,
llList2String(gNodes, UNPACK_REL_SRC(rel)),
(key)llList2String(gNodes, UNPACK_REL_DST(rel))
);
if ((i % 25) == 0)
llSleep(0.5);
}
llMessageLinked(LINK_THIS, IPC_FINISH_SAVE_INWORLD_STATE, "", NULL_KEY);
gGraphDirty = FALSE;
}
clearAll() {
llRegionSay(NODE_CHANNEL, "node_kill_all:" + gOwnLogicalID);
llRegionSay(NODE_CHANNEL, "arrow_kill_all:" + gOwnLogicalID);
gNodes = [];
gNodeRelations = [];
gPendingNodes = [];
gPendingRelations = [];
gArrowPool = [];
gNeededArrows = 0;
gNeededNodes = 0;
gAvailRezSlots = MAX_REZ_SLOTS;
}
initRestoreNodes(integer restore_arrows) {
clearAll();
gRestoring = TRUE;
gRestoreArrows = restore_arrows;
gLastRezTime = llGetUnixTime();
llMessageLinked(LINK_THIS, IPC_REQUEST_RESTORE_FROM_DATA, "", NULL_KEY);
}
addRelation(string src, string dst) {
list rel = [PACK_RELATION_FROM_IDS(gNodes, src, dst)];
if (llListFindList(gNodeRelations, rel) == -1) {
gPendingRelations += rel;
++gNeededArrows;
tryRezPending();
}
}
removeRelation(string src, string dst) {
list rel = [PACK_RELATION_FROM_IDS(gNodes, src, dst)];
integer idx = llListFindList(gNodeRelations, rel);
if (idx != -1) {
gNodeRelations = llDeleteSubList(gNodeRelations, idx, idx);
}
llRegionSay(NODE_CHANNEL, "arrow_kill:" + src + ":" + dst);
}
updateNodeRelations() {
if (gPendingRelations == [])
return;
integer i;
integer len = llGetListLength(gPendingRelations);
for(i=len-1; i>=0; --i) {
if (gArrowPool == [])
return;
integer rel = llList2Integer(gPendingRelations, i);
string src_id = llList2String(gNodes, UNPACK_REL_SRC(rel));
string dst_id = llList2String(gNodes, UNPACK_REL_DST(rel));
string src_key = nodeIDToKey(src_id);
string dst_key = nodeIDToKey(dst_id);
if (src_key != NULL_KEY && dst_key != NULL_KEY) {
string msg = llDumpList2String(["arrow_add", src_id, src_key, dst_id, dst_key, gOwnLogicalID], ":");
llRegionSayTo(llList2Key(gArrowPool, 0), NODE_CHANNEL, msg);
gPendingRelations = llDeleteSubList(gPendingRelations, i, i);
gArrowPool = llDeleteSubList(gArrowPool, 0, 0);
gNodeRelations += [rel];
}
}
}
tryRezPending() {
integer i;
while (gNeededNodes > 0 && gAvailRezSlots > 0) {
llMessageLinked(LINK_SET, IPC_REZ_PRIM, (string)(++gRezNum), (key)"node");
--gAvailRezSlots;
--gNeededNodes;
}
while (gNeededArrows > 0 && gAvailRezSlots > 0) {
llMessageLinked(LINK_SET, IPC_REZ_PRIM, (string)(++gRezNum), (key)"arrow");
--gAvailRezSlots;
--gNeededArrows;
}
// llOwnerSay("rez " + llDumpList2String([gAvailRezSlots, gNeededArrows, gNeededNodes], "|"));
}
handleNodeDuplicated(key existing_key, key node_key, string node_id) {
// Ask the original object to give itself a new ID.
string gen_node_id = generateLogicalID();
llRegionSayTo(existing_key, NODE_CHANNEL, "node_reset:" + gen_node_id + ":" + gOwnLogicalID);
// rebind node_id to the new object's key
integer node_idx = llListFindList(gNodes, [node_id]);
integer node_key_idx = node_idx + NODE_KEY_OFFSET;
gNodes = llListReplaceList(gNodes, [compressKey(node_key)], node_key_idx, node_key_idx);
// Tell any arrows to start pointing at the new object
llRegionSay(NODE_CHANNEL, "arrow_node_identity:" + node_id + ":" + (string)node_key);
// add the node back to the list with its new ID
trackNode(gen_node_id, existing_key);
// make a bidirectional relationship between the two nodes since that's what
// you'll want most of the time.
addRelation(gen_node_id, node_id);
addRelation(node_id, gen_node_id);
}
handleNodeRenamed(string existing_id, key node_key, string node_id) {
// kill all relations, too annoying to deal with for now.
untrackNode(existing_id, TRUE);
trackNode(node_id, node_key);
}
default {
state_entry() {
restoreLogicalID(TRUE);
gMenuChannel = MENU_CHANNEL_BASE ^ llHash(llGetObjectDesc());
gConnectionMenuChannel = CONNECTION_MENU_CHANNEL_BASE ^ llHash(llGetObjectDesc());
llListen(NODE_MASTER_CHANNEL, "", "", "");
llListen(PATHFINDING_REQ_CHANNEL, "", "", "");
llListen(gMenuChannel, "", "", "");
llListen(gConnectionMenuChannel, "", "", "");
llSetColor(<1, 0, 1>, ALL_SIDES);
clearAll();
llSetTimerEvent(1);
}
on_rez(integer start_param) {
llResetScript();
}
listen(integer channel, string name, key id, string msg) {
// only non-owner messages we want to reply to are path requests
if (llGetOwnerKey(id) != llGetOwner() && channel != PATHFINDING_REQ_CHANNEL)
return;
list params = llParseStringKeepNulls(msg, [":"], []);
string cmd = llList2String(params, 0);
params = llDeleteSubList(params, 0, 0);
if (channel == NODE_MASTER_CHANNEL) {
key node_key = id;
if (cmd == "node_alive") {
// A node just came on the scene and is telling us about itself
string parent_id = llList2String(params, 1);
if (parent_id != "" && parent_id != gOwnLogicalID) {
return;
}
string node_id = llList2String(params, 0);
string existing_id = nodeKeyToID(node_key);
key existing_key = nodeIDToKey(node_id);
if (existing_id == "" && existing_key == NULL_KEY) {
trackNode(node_id, node_key);
llRegionSayTo(node_key, NODE_CHANNEL, "node_reset:" + node_id + ":" + gOwnLogicalID);
gGraphDirty = TRUE;
} else if (existing_id != "" && existing_id != node_id) {
// an in-world object we were tracking is now claiming to represent a different node?
llOwnerSay("node hello from " + (string)node_key + " had mismatch, " + node_id + " != " + existing_id);
handleNodeRenamed(existing_id, node_key, node_id);
gGraphDirty = TRUE;
} else if (existing_key != node_key) {
// node ID collides with an existing node object. Probably happened due to shift drag
// duplicate. That leaves the _original_ object selected and leaves a duplicate in the
// old position.
handleNodeDuplicated(existing_key, node_key, node_id);
gGraphDirty = TRUE;
}
} else if (cmd == "node_touched") {
string clicked_id = nodeKeyToID(node_key);
// not our node.
if (clicked_id == "")
return;
if (llList2String(params, 0) != gOwnLogicalID)
return;
key toucher_id = llList2Key(params, 1);
if (toucher_id != llGetOwner())
return;
if (gFirstClicked != "" && gSecondClicked != "") {
// the dialog triggered, but they probably hit ignore.
// treat this as if it was a click on a first node.
gFirstClicked = clicked_id;
gSecondClicked = "";
} else if (gFirstClicked != "") {
if (clicked_id != gFirstClicked) {
gSecondClicked = clicked_id;
llDialog(toucher_id, "What do you want to do with the connection?", ["one-way", "two-way", "remove", "find path"], gConnectionMenuChannel);
}
} else {
gFirstClicked = clicked_id;
}
gClickTimeout = llGetUnixTime() + 20;
}
} else if (channel == gMenuChannel) {
if (msg == "dump") {
dumpNodeDetails();
} else if (msg == "save") {
if (llGetListLength(gNodes)) {
saveNodes();
} else {
llOwnerSay("Refusing to save empty node list");
}
} else if (msg == "restore") {
initRestoreNodes(TRUE);
} else if (msg == "rest. norel") {
initRestoreNodes(FALSE);
} else if (msg == "clear") {
clearAll();
llResetScript();
}
} else if (channel == gConnectionMenuChannel) {
if (msg == "remove") {
removeRelation(gFirstClicked, gSecondClicked);
removeRelation(gSecondClicked, gFirstClicked);
gGraphDirty = TRUE;
} else if (msg == "one-way") {
addRelation(gFirstClicked, gSecondClicked);
removeRelation(gSecondClicked, gFirstClicked);
gGraphDirty = TRUE;
} else if (msg == "two-way") {
addRelation(gFirstClicked, gSecondClicked);
addRelation(gSecondClicked, gFirstClicked);
gGraphDirty = TRUE;
} else if (msg == "find path") {
if (gGraphDirty) {
llOwnerSay("Refusing to find path, graph is dirty");
} else {
llOwnerSay("Calculating path from " + gFirstClicked + " to " + gSecondClicked);
llMessageLinked(LINK_THIS, IPC_REQUEST_PATH, gFirstClicked + ":" + gSecondClicked, NULL_KEY);
}
}
gFirstClicked = "";
gSecondClicked = "";
gClickTimeout = 0;
} else if (channel == PATHFINDING_REQ_CHANNEL) {
integer num;
if (cmd == "find_path")
num = IPC_REQUEST_PATH;
else if (cmd == "find_path_vectors")
num = IPC_REQUEST_PATH_VECTORS;
else
return;
// they weren't even asking us.
if (gOwnLogicalID == "" || llList2String(params, 0) != gOwnLogicalID) {
return;
}
string link_msg = llList2String(params, 1) + ":" + llList2String(params, 2);
llMessageLinked(LINK_THIS, num, link_msg, id);
}
}
link_message(integer sender_num, integer num, string str, key id) {
if (num == IPC_RESTORE_NODE) {
gPendingNodes += [str, (vector)((string)id)];
++gNeededNodes;
} else if (num == IPC_RESTORE_RELATION) {
// Don't try to rez the arrow yet, we expect more data to come in.
// gPendingNodes should end up with the same order and stride length
// as gNodes, so this is fine.
gPendingRelations += [PACK_RELATION_FROM_IDS(gPendingNodes, str, (string)id)];
++gNeededArrows;
} else if (num == IPC_FINISH_RESTORE_FROM_DATA) {
if (!gRestoreArrows) {
gNeededArrows = 0;
gNodeRelations = gPendingRelations;
gPendingRelations = [];
}
llOwnerSay("Got data from data manager, rezzing...");
tryRezPending();
gGraphDirty = FALSE;
} else if (num == IPC_PATH_RESPONSE) {
if (id) {
llRegionSayTo(id, PATHFINDING_RESP_CHANNEL, "path:" + str);
} else {
llOwnerSay("PATH:");
llOwnerSay(str);
}
}
}
object_rez(key id) {
// a rez completing frees up a rez slot
gLastRezTime = llGetUnixTime();
++gAvailRezSlots;
if (llGetOwnerKey(id) != llGetOwner()) {
llOwnerSay("Failed a rez?");
return;
}
string name = (string)llGetObjectDetails(id, [OBJECT_NAME]);
if (name == "node") {
if (gPendingNodes == []) {
llOwnerSay("Rezzed node with no pending nodes???");
return;
}
string node_id = llList2String(gPendingNodes, NODE_ID_OFFSET);
vector node_pos = llList2Vector(gPendingNodes, NODE_POS_OFFSET);
llRegionSayTo(id, NODE_CHANNEL, "node_assign:" + node_id + ":" + gOwnLogicalID + ":" + (string)node_pos);
gPendingNodes = llDeleteSubList(gPendingNodes, 0, NODE_STRIDE - 1);
trackNode(node_id, id);
} else if (name == "arrow") {
if (gPendingRelations == []) {
llOwnerSay("Rezzed arrow with no pending relations???");
return;
}
gArrowPool += [id];
updateNodeRelations();
}
if (gNeededArrows || gNeededNodes || llGetListLength(gPendingNodes) || llGetListLength(gPendingRelations)) {
tryRezPending();
} else if (gRestoring) {
gRestoring = FALSE;
llOwnerSay("Done rez");
}
}
touch_start(integer touch_num) {
if (llDetectedKey(0) != llGetOwner())
return;
llDialog(llDetectedKey(0), "Node Management", ["dump", "save", "restore", "rest. norel", "clear"], gMenuChannel);
}
timer() {
++gTickCount;
// did the owner change our identity by editing the object description
if (llGetObjectDesc() != gOwnLogicalID + ":") {
if (gOwnLogicalID != "") {
// get rid of any existing nodes or arrows
clearAll();
}
llResetScript();
}
if (gClickTimeout && gClickTimeout < llGetUnixTime()) {
gFirstClicked = "";
gSecondClicked = "";
gClickTimeout = 0;
}
if (!(gTickCount % PRUNE_NODES_EVERY)) {
pruneNodes();
}
if (gNeededArrows || gNeededNodes) {
// something screwed up and didn't even give us a failed rez message
// parcel may be full.
if (llGetUnixTime() > gLastRezTime + 10) {
gNeededNodes = 0;
gNeededArrows = 0;
gRestoring = FALSE;
llOwnerSay("Restore got stuck, inconsistent state! Is the parcel full?");
}
}
updateNodeRelations();
}
}