aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/media/Test sim/objects/onefang%27s%20test%20bed.5cb927d5-1304-4f1a-9947-308251ef2df0/~menu.lsl
blob: ea19396f66eb22045b445bdd7a0d969d56be3363 (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
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
// MLPV2 Version 2.3, by Learjeff Innis, based on
// MLP MULTI-LOVE-POSE V1.2 - Copyright (c) 2006, by Miffy Fluffy (BSD License)
// 15-color balls by Lizz Silverstar
// OpenSim port by Jez Ember
// Meta 7 fixes by onefang Rejected

// 2.3: sequences
//      6 avs
//      Adjusting state

integer MAX_BALLS   = 6;

string  Version      = "MLPV2.5";

integer b;
integer b0;
integer ballusers;
list    BallColors;
integer ch;
integer chat = 1;
integer group;
integer i;
integer menu;
integer menuusers;
integer redo = 1;
integer swap;
integer visible;
integer BallCount;
integer SaneMenuOrder;
integer ReloadOnRez;

integer Adjusting;
string  LastPose;

integer BallsNeeded;

float alpha;
string cmd;
string pose;
string Posemsg;     // for 'AGAIN'
key owner;
key user;
key user0;
list buttons;
list buttonindex;
list commands;
list menus;
list balls;
list users;

list SoundNames;
list Sounds;
list LMButtons;
list LMParms;
list MenuStack = [0];     // indices to previous menus, for "BACK" command

integer MenuPage;       // which page of current menu we're on, 0 for first

// Replacement for llListFindList which is currently broken in OSLSL
integer myListFind(list a, string b) {
    integer x;
    integer l=llGetListLength(a);

    for(x=0; x<l; x++) {
        if(llList2String(a,x) == b) {
            return x;
        }
    }
    
    return -1;
}

stop() {
    sendStand();     //msg to pos/pose
    llMessageLinked(LINK_THIS, 1, "STOP", (key)"");
    llSleep(0.2);
    killBalls();
    swap = 0;
    Adjusting = FALSE;
}


check_poses() {
    llOwnerSay("Checking configs");
    llMessageLinked(LINK_THIS,0,"POSEB", (key)"CHECK1");                        //msg to memory
    integer ix;
    string name;
    for (ix = 0; ix < llGetListLength(buttons); ++ix) {
        name = llList2String(buttons, ix);
        if (((integer)llList2String(commands, ix)) != 0) {
            llMessageLinked(LINK_THIS,0,"POSEB", (key)name);                    //msg to memory
        }
    }
    llMessageLinked(LINK_THIS,0,"POSEB", (key)"CHECK2");                        //msg to memory
}

// Return a channel number that's based on the prim's key -- unique per object
integer channel() {
    return (integer)("0x"+llGetSubString((string)llGetKey(),-4,-1));
}


// setup for a pose based on menu characteristics
setup_pose() {
    if (BallsNeeded) {                  // if submenu includes balls:
        if (BallCount != BallsNeeded) {
            rezBalls();       // if not enough balls present: create balls
            llSleep(0.5);
        }
        integer ix;
        for (ix = 0; ix < BallsNeeded; ++ix) {
            llSay(ch + ix, llList2String(BallColors, ix)     // to ball: color, ballnum, adjusting
                + "|" + (string) ix
                + "|" + (string) Adjusting);
        }
        if (ballusers) setBalls("GROUP");           //if group access only
    }
}


unauth(string button, string who) {
        llDialog(user0, "\n" + button + " button allowed only for " + who, ["OK"], -1);
}

continMenu(string str) {
    llDialog(user0, "\n"+str+llKey2Name(user)+" is using the menu, continue?", ["Yes","Cancel"], ch - 1);
}

mainMenu() {
    MenuPage = 0;
    menu = 0;
    doMenu(FALSE);
}

//menu partly based on Menu Engine by Zonax Delorean (BSD License)
//llDialog(user, menuname, buttons(from index to nextindex-1), channel)  
doMenu(integer inhibit_showing) {
    integer colors = llList2Integer(balls,menu);
    integer ix;
    integer mask = 0xf;
    integer shift = 0;
    BallsNeeded = 0;
    BallColors = [];

    for (ix = 0; ix < MAX_BALLS; ++ix) {
        integer bc = (colors & mask) >> ix*4;
        BallColors += (list)bc; 
        if (bc) {
            BallsNeeded += 1;
        }
        mask = mask << 4;
    }
    
    if (inhibit_showing) {
        return;
    }

    b0 = llList2Integer(buttonindex, menu);         //position of first button for this (sub)menu
    b = llList2Integer(buttonindex, menu+1);        //position of first button for next (sub)menu

    b0 += MenuPage * 12;
    if (b - b0 > 12) {
        b = b0 + 12;
    }
    
    list buttons1;
    buttons1 = llList2List(buttons, b0, b - 1);
    if (SaneMenuOrder) {
        buttons1 =
              llList2List(buttons1, -3, -1)
            + llList2List(buttons1, -6, -4)
            + llList2List(buttons1, -9, -7)
            + llList2List(buttons1, -12, -10);
    }
    llDialog(user, Version + "\n\n" + llList2String(menus,menu), buttons1, ch - 1);
    llResetTime();
}

say(string str) {
    if (menuusers) llWhisper(0,str);
    else llOwnerSay(str);
}

killBalls() {
    integer ix;
    for (ix = 0; ix < MAX_BALLS; ++ix) {
        llSay(ch + ix, "DIE");      //msg to balls
    }
    BallCount = 0;
    llSetTimerEvent(0.0);
}

setBalls(string cmd) {
    integer ix;
    for (ix = 0; ix < BallCount; ++ix) {
        llSay(ch + ix, cmd);      //msg to balls
    }
}


rezBalls() {
    integer current = BallCount;

    if (BallsNeeded == BallCount) return;

    if (BallCount == 0) {
        killBalls();  // for reinitialization, if old balls are around
    }

    while (BallCount > BallsNeeded) {
        --BallCount;
        llSay(ch + BallCount, "DIE");
    }
    
    while (BallCount < BallsNeeded) {
        llRezObject("~ball",llGetPos(),ZERO_VECTOR,ZERO_ROTATION,ch+BallCount);
        ++BallCount;
    }
    
    // Only do this if there were no balls
    if (! current) {
        llMessageLinked(LINK_THIS,0,"REPOS",(key)"");  //msg to pos
    }
    
    llSetTimerEvent(30.0);
}



sendStand() {
    llMessageLinked(LINK_THIS,0,"POSE","0,"+(string)BallCount);        //msg to pos/pose
    llMessageLinked(LINK_THIS,0,"POSEB", "stand");
}

touched(integer same_group) {
    if (user0 == owner || (menuusers == 1 && same_group) || menuusers == 2) {   //0=owner 1=group 2=all
        if (user0 != user) {
            if (llGetTime() < 60.0 && user != (key)"") {
                continMenu("");
                return;
            }
            user = user0;
            group = same_group;
        }
        mainMenu();
    }
}


// return TRUE if caller should do menu
integer handle_cmd(string button, integer sequenced) {
    b = myListFind(buttons, button);                     //find position of cmd
    string cmd = llList2String(commands,b);                         //get command

    if (cmd == "TOMENU") {
        integer newmenu = myListFind(menus, button);         //find submenu
        if (newmenu == -1) return FALSE;
        if (sequenced) {
            integer oldmenu = menu;
            menu = newmenu;
            doMenu(TRUE);
            setup_pose();
            menu = oldmenu;
            return FALSE;
        }
        i = llList2Integer(users, newmenu); 
        if (user == owner || (i == 1 && group) || i == 2) {         //0=owner 1=group 2=all
            MenuStack = (list)menu + MenuStack;
            MenuPage = 0;
            menu = newmenu;
            doMenu(sequenced);
            return FALSE;
        }
        if (i == 1) unauth(button, "group");
        else unauth(button, "owner");
        return FALSE;
    } else if (cmd == "BACK") {
        if (MenuPage) {
            --MenuPage;                                           
            doMenu(sequenced);
            return FALSE;
        }
        menu = llList2Integer(MenuStack,0);
        MenuStack = llList2List(MenuStack,1,-1);
        doMenu(sequenced);
        return FALSE;
    } else if (cmd == "MORE") {
        ++MenuPage;
        doMenu(sequenced);
        return FALSE;
    } else if (cmd == "CHECK") {
        check_poses();
    } else if ((integer)cmd > 0) {                                  //POSE
        if (Adjusting && button != pose) {
            llMessageLinked(LINK_THIS,0,"SAVE",pose);               //msg to pos/pose
            llSleep(5.);
        }
        setup_pose();
        Posemsg = cmd + "," + (string) BallCount;
        llMessageLinked(LINK_THIS,0,"POSE", Posemsg);               //msg to pose
        llMessageLinked(LINK_THIS,0,"POSEB", (key)button);          //msg to memory
        if (chat) say(button);
        pose = button;
    } else if (cmd == "SWAP") {
        swap += 1;
        llMessageLinked(LINK_THIS,0,"SWAP",(key)((string)swap));              //msg to pos/pose
    } else if (cmd == "STAND") {
        sendStand();     //msg to pos/pose
        if (chat) say(button);
        pose = "stand";
    } else if (cmd == "STOP") {
        if (chat) say(button);
        stop();
        return FALSE;
    } else if (cmd == "ADJUST") {
        Adjusting = ! Adjusting;
        setBalls("ADJUST|" + (string)Adjusting);
    } else if (cmd == "HIDE") {
        setBalls("0");
    } else if (cmd == "SHOW") {
        setBalls("SHOW");
    } else if (cmd == "DUMP") {
        llMessageLinked(LINK_THIS,1,"DUMP",(key)"");
    } else if (cmd == "INVISIBLE") {
        visible = !visible;
        llSetAlpha((float)visible*alpha, ALL_SIDES);
    } else if (cmd == "REDO") {
        redo = !redo;
        if (redo) say(button+" ON"); else say(button+" OFF");
    } else if (cmd == "CHAT") {
        chat = !chat;
        if (chat) say(button+" ON"); else say(button+" OFF");
    } else if (cmd == "BALLUSERS") {
        ballusers = !ballusers;
        if (ballusers) {
            llOwnerSay(button+" GROUP");
            setBalls("GROUP");
        } else {    
            llOwnerSay(button+" ALL");
            setBalls("ALL");
        }
    } else if (cmd == "MENUUSERS") {
        if (user == owner) {
            if (!menuusers) {
                menuusers = 1;
                llOwnerSay(button+" GROUP");
            } else if (menuusers == 1) {
                menuusers = 2;
                llOwnerSay(button+" ALL");
            } else if (menuusers == 2) {
                menuusers = 0;
                llOwnerSay(button+" OWNER");
            }
        } else unauth(button, "owner");
    } else if (cmd == "RESET" || cmd == "RELOAD" || cmd == "RESTART") {
        stop();
        if (chat) say(button);
        if (cmd == "RESET") {
            llResetScript();
        } else {
            llResetOtherScript("~memory");
            if (cmd == "RESTART") {
                llResetScript();
            }
        }
    } else if (cmd == "OFF") {
        sendStand();     //msg to pos/pose
        stop();
        if (user == owner) {
            llOwnerSay(button);
            llResetOtherScript("~run");
            llResetScript();
        }
        unauth(button, "owner");
        return FALSE;
    } else if (llGetSubString(cmd, 0, 0) == "Z" || (cmd == "SAVE")) {    //SAVE or Z-adjust
        llSay(0,"Z");
        llMessageLinked(LINK_THIS,0,cmd,pose);                           //msg to pos/pose
        doMenu(sequenced);
        return FALSE;
    } else if (cmd == "LINKMSG") {
        // menu button to send LM to a non-MLPV2 script
        integer ix = myListFind(LMButtons, button);
        if (ix != -1) {
            list lmparms = llCSV2List(llList2String(LMParms, ix));
            llMessageLinked(
                llList2Integer(lmparms, 1),     // destination link#
                llList2Integer(lmparms, 2),     // 'num' arg
                llList2String(lmparms, 3),      // 'str' arg
                user0);                         // key arg
            if (llList2Integer(lmparms,0)) {    // inhibit remenu?
                return FALSE;                         //   yes, bug out
            }
        }
    } else if (cmd == "SOUND") {
        integer ix = myListFind(SoundNames, button);
        if (ix >= 0) {
            llPlaySound(llList2String(Sounds, ix), 1.);
        }
    }
    return TRUE;
}

default {
    state_entry() {
        ch = channel();
        killBalls();
        llSleep(2.0);       // give ~run a chance to shut us down
        llResetOtherScript("~menucfg");
        llResetOtherScript("~pos");
        llResetOtherScript("~pose");
        llResetOtherScript("~poser");
        llResetOtherScript("~poser 1");
        llResetOtherScript("~poser 2");
        llResetOtherScript("~poser 3");
        llResetOtherScript("~poser 4");
        llResetOtherScript("~poser 5");
        alpha = llGetAlpha(0);                                          //store object transparancy (alpha)
        if (alpha < 0.1) alpha = 0.5; else visible = 1;                 //if invisible store a visible alpha
    }

    link_message(integer from, integer num, string str, key id) {
        if (from != llGetLinkNumber()) { return; }
        if (num >= 0) { return;}
        
        // LMs from ~memory, passing configuration
        if (num == -1) {
            buttons = llCSV2List(str);
        } else if (num == -2) {
            commands = llCSV2List(str);
        } else if (num == -3) {
            menus = llCSV2List(str);
        } else if (num == -4) {
            buttonindex = llCSV2List(str);
        } else if (num == -5) {
            balls = llCSV2List(str);
        } else if (num == -6) {
            users = llCSV2List(str);
        } else if (num == -7) {
            LMButtons = llCSV2List(str);
        } else if (num == -8) {
            LMParms = llParseStringKeepNulls(str, ["|"], []);
        } else if (num == -9) {
            SoundNames = llCSV2List(str);
        } else if (num == -10) {
            Sounds = llCSV2List(str);
        } else if (num == -20) {
            list args = llCSV2List(str);
            redo            = llList2Integer(args,0);
            chat            = llList2Integer(args,1);
            ballusers       = llList2Integer(args,2);
            menuusers       = llList2Integer(args,3);
            SaneMenuOrder   = llList2Integer(args,4);
            ReloadOnRez     = llList2Integer(args,5);

            state on;
        }
    }

    state_exit() {
        llOwnerSay("("+llGetScriptName()+": "+(string)llGetFreeMemory()+" bytes free)");
        llWhisper(0, Version + ": READY");
    }
}

state re_on {
    state_entry() {
        state on;
    }
}

state on {
    state_entry() {
        ch = channel();
        owner = llGetOwner();
        llListen(ch - 1, "", NULL_KEY, "");                      //listen for pressed buttons
        // llWhisper(0, "Channel: " + (string)ch);
    }
    
    on_rez(integer arg) {
        if (ReloadOnRez) {
            llResetScript();
        }
        BallCount = 0;
        llSetTimerEvent(0.0);
        state re_on;
    }

    touch_start(integer tcount) {
        user0 = llDetectedKey(0);
        touched(llDetectedGroup(0));
    }
    
    listen(integer channel, string name, key id, string button) {
        if (id != user) {
            if (button == "Yes") {
                user = id;
                group = llSameGroup(user0);
                mainMenu();
            } else if (button != "Cancel") {
                continMenu("Selection cancelled because ");
            }
            return;
        }
        if (handle_cmd(button, FALSE) && redo) doMenu(FALSE);
    }


    link_message(integer from, integer num, string str, key id) { 
        if (str == "PRIMTOUCH") {
            user0 = id;
            touched(num);
            return;
        }
        if (num == 0 && str == "AGAIN") {
            llMessageLinked(LINK_THIS,0,"POSE", Posemsg);           //msg to pose
            llMessageLinked(LINK_THIS,0,"POSEB", (key)pose);        //msg to memory
            return;
        }
        if (num == -12002) {
            handle_cmd(str, TRUE);
            return;
        }
    }

    timer() {
        setBalls("LIVE");           //msg to balls: stay alive
    }
}