aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL/Test sim/objects/onefang%27s%20test%20bed.5cb927d5-1304-4f1a-9947-308251ef2df0/~props.lsl
blob: a731d77f7d6523dfe52e9d23e3b6c665d251f04f (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
//Imported by X-Avatar.de
//MPLV2 Version 2.3 by Learjeff Innis, based on
//MLP MULTI-LOVE-POSE V1.2 - Copyright (c) 2006, by Miffy Fluffy (BSD License)


// v2.2 - rotate all props

// This script handles props (object rezzed for a given pose)

integer Checking = FALSE;       // whether doing consistency check

integer Line;
integer PoseCount;
list    Poses;                  // name of each pose with a prop
list    Positions;              // position of prop, indexed as Poses
list    Props;                  // name of each prop object, indexed as Poses

string  Pose;                   // current pose name

integer ChatChan;               // chan for talking to object

init()
{
    ChatChan = - 1 - (integer)llFrand(-DEBUG_CHANNEL);
    getRefPos();
}

vector RefPos;
rotation RefRot;

getRefPos() {   //reference position
    RefPos = llGetPos();
    RefRot = llGetRot();
    integer z = (integer)llGetObjectDesc();
    RefPos.z += (float)z/100.0;
}

string plural(string singular, string plural, integer count) {
    if (count != 1) {
        return plural;
    }
    return singular;
}

announce()
{
    llOwnerSay((string)PoseCount
        + plural(" pose", " poses", PoseCount)
        + " with props ("
        + llGetScriptName()
        + ": "
        + (string)llGetFreeMemory()
        + " bytes free)");
}

string adjust(integer doOffset, vector pos, vector erot, vector amt) {
    if (doOffset) {
        pos += amt/100.;
        return (vround(pos) + "/" + vround(erot));
    }
    rotation amount = llEuler2Rot(amt * DEG_TO_RAD);
    erot *= DEG_TO_RAD;
    
    rotation oldrot = llEuler2Rot(erot);
    rotation newrot = oldrot / amount;
    
    erot = llRot2Euler(newrot) * RAD_TO_DEG;
    pos = pos / amount;
    return(vround(pos) + "/" + vround(erot));
}


adjust_all(integer doOffset, vector amt) {
    integer ix;
    integer bx;
    string  data;
    list    ldata;
    vector  pos;
    vector  erot;
    

    for (ix = 0; ix < llGetListLength(Poses); ++ix) {
        data = llList2String(Positions, ix);
        ldata = llParseString2List(data, ["/"], []);
        pos = (vector)llList2String(ldata, 0);
        erot = (vector)llList2String(ldata, 1);
        
        data = adjust(doOffset, pos, erot, amt);
        Positions = llListReplaceList(Positions, (list)data, ix, ix);
    }
    llOwnerSay("Prop rotation complete");
}



string roundData(string data) {
    list ldata = llParseString2List(data, ["/"], []);
    return (vround((vector)llList2String(ldata, 0)) + "/" + vround((vector)llList2String(ldata, 1)));
}

// round a vector's components and return as vector value string
string vround(vector vec) {
    return ("<"+round(vec.x, 3)+","+round(vec.y, 3)+","+round(vec.z, 3)+">");
}

string round(float number, integer places) {
    float shifted;
    integer rounded;
    string s;
    shifted = number * llPow(10.0,(float)places);
    rounded = llRound(shifted);
    s = (string)((float)rounded / llPow(10.0,(float)places));
    s = llGetSubString(s,0,llSubStringIndex(s, ".")+places);
    return s;
}


add_prop(string pose, string prop, string data) {
    if (llListFindList(Props, (list)pose) != -1) {
        llOwnerSay("Multiple *.PROPS* entries for pose '" + pose + "'");
    }
    if (llGetInventoryType(prop) != INVENTORY_OBJECT) {
        llOwnerSay("Warning: can't find prop '" + prop + "' in inventory");
    }
    Poses = (list) pose;
    Props =  (list) prop;
    Positions =  (list) roundData(data);
    ++PoseCount;
}

string get_pose_data(string pose)
{
    integer ix = llListFindList(Poses, (list)pose);

    if (ix == -1) {
        return "";
    }

    return llList2String(Positions, ix);
}

save_prop(string pose, string prop, string data) {
    integer ix = llListFindList(Poses, [pose]);
    if (ix == -1) {
        // llSay(0, "new pose");
        add_prop(pose, prop, data); // don't expect this to happen
        return;
    }
    
    // if the prop object name doesn't match, ignore it -- assume it's noise
    if (llList2String(Props, ix) != prop) {
        return;
    }
    
    // Data is the change in position since we rezzed it, in global coords
    // Convert delta to local axes
    
    list ldata = llParseString2List(data, ["/"], []);
    vector pos = (vector) llList2String(ldata, 0);
    rotation rot = (rotation)llList2String(ldata, 1);
    
    pos = pos / RefRot;
    vector erot = llRot2Euler(rot/RefRot) * RAD_TO_DEG;
    
    // Now add to saved data (since it was a delta)
    ldata = llParseStringKeepNulls(llList2String(Positions, ix), ["/"], []);
    vector oldpos = (vector)llList2String(ldata, 0);
    
    pos += oldpos;
    data = vround(pos) + "/" + vround(erot);

    Props     = llListReplaceList(Props,     (list)prop, ix, ix);
    Positions = llListReplaceList(Positions, (list)data, ix, ix);
    
    string name = llGetObjectName();
    llSetObjectName("");
    llOwnerSay("| " + pose + " | " + prop + " | " + data);
    llSetObjectName(name);
}

rez_prop(string pose) {
    llSay(ChatChan, "DIE");
    integer ix = -1; //(integer)llListFindList(Poses, [pose]);
    integer ggt =0;
                    for (ggt=0; ggt < llGetListLength(Poses); ggt++)
                    {
                        if (llList2String(Poses,ggt) == pose)
                        {
                            ix = ggt;
                            ggt = llGetListLength(Poses);
                        }
                    }
    if (ix == -1)
    {
        Pose = "";
        llSetTimerEvent(0.0);
        return;
    }
    string prop = llList2String(Props, ix);
    string data = llList2String(Positions, ix);
    list ldata = llParseString2List(data, ["/"], []);
    vector pos = (vector)llList2String(ldata, 0);
    vector erot = (vector)llList2String(ldata, 1);

    pos = pos * RefRot + RefPos;
    rotation rot = llEuler2Rot(erot*DEG_TO_RAD) * RefRot;
    
    // llSay(0, "rezzing '" + prop + "' at " + (string) pos);
    llRezAtRoot(prop, pos, <0.,0.,0.>, rot, ChatChan);
    llSetTimerEvent(60.0);
    Pose = pose;
}


dashes() {
    llOwnerSay("_______________________________________________________________________________");
    llOwnerSay("");
}


// Globals for reading card config
integer ConfigLineIndex;
list    ConfigCards;        // list of names of config cards
string  ConfigCardName;     // name of card being read
integer ConfigCardIndex;    // index of next card to read
key     ConfigQueryId;

integer next_card()
{
    if (ConfigCardIndex >= llGetListLength(ConfigCards)) {
        ConfigCards = [];
        return (FALSE);
    }
    
    ConfigLineIndex = 0;
    ConfigCardName = llList2String(ConfigCards, ConfigCardIndex);
    ConfigCardIndex++;
    ConfigQueryId = llGetNotecardLine(ConfigCardName, ConfigLineIndex);
    llOwnerSay("Reading " + ConfigCardName);
    return (TRUE);
}                             


default {
    state_entry() {
        llSleep(0.25);       // give ~run a chance to shut us down
        string item;
        ConfigCards = [];
        integer n = llGetInventoryNumber(INVENTORY_NOTECARD);
        while (n-- > 0) {
            item = llGetInventoryName(INVENTORY_NOTECARD, n);
            if (llSubStringIndex(item, ".PROPS") != -1) {
                ConfigCards = (list) item;
            }
        }

        ConfigCardIndex = 0;
        ConfigCards = llListSort(ConfigCards, 1, TRUE);
        next_card();
    }
    
    dataserver(key query_id, string data2) {
        if (query_id != ConfigQueryId) {
            return;
        }                                
        if (data2 == EOF) {
            if (next_card()) {
                return;
            }
            state on;
        }

        string data3 = llStringTrim(data2, STRING_TRIM);
        if (llGetSubString(data3,0,0) != "/" && llStringLength(data3)) {          // skip comments and blank lines

            list ldata = llParseStringKeepNulls(data3, ["  |  ","  | "," |  "," | "," |","| ","|"], []);
            if (llGetListLength(ldata) != 4) {
                llOwnerSay(llGetScriptName() + ": error in " + ConfigCardName + ":" + (string)ConfigLineIndex
                    + " - need exactly 3 vertical bars - line ignored");
            } else {
                string pose = llList2String(ldata, 1);
                string prop = llList2String(ldata, 2);
                string data = llStringTrim(llList2String(ldata, 3), STRING_TRIM);
                if (data3 == "") {
                    data3 = "<0,0,1>/<0,0,0>";
                }
                add_prop(pose, prop, data3);
            }
        }
        ++ConfigLineIndex;
        ConfigQueryId = llGetNotecardLine(ConfigCardName, ConfigLineIndex);       //read next line of positions notecard
    }

    state_exit() {
        // do one save to indicate actual amount of available memory
        string position = llList2String(Positions, 0);
        Positions = llListReplaceList(Positions, [position],0,0);

        llMessageLinked(LINK_THIS, 2, "OK", (key)""); //msg to menu, in case it's waiting for loading
        announce();
    }
}

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

state on {
    state_entry() {
        init();
        llListen(ChatChan, "", NULL_KEY, "");
    }

    on_rez(integer arg) {
        state re_on;
    }
    
    listen(integer chan, string name, key id, string msg) {
        // llSay(0, name + ": " + msg);
        if (Pose == "") {
            return;
        }
        save_prop(Pose, name, msg);
        announce();
    }

    link_message(integer from, integer num, string str, key dkey) {

        if (str == "PRIMTOUCH" || num < 0) {
            return;
        }
        
        if (num == 0) {
            if (str == "POSEB") {
                rez_prop((string)dkey);
                return;
            }
            if (str == "REPOS") {
                getRefPos();
                return;
            }
            return;
        }

        if (num != 1) {
            return;
        }
        
        if (str == "STOP") {
            llSay(ChatChan, "DIE");
            Pose = "";
            llSetTimerEvent(0.0);
        }

        if (str == "DUMPPROPS") {
            dashes();
            llOwnerSay("Copy to .PROPS; delete any other *.PROPS* cards");
            dashes();
            string name = llGetObjectName();
            llSetObjectName("");

            integer ix;
            for (ix = 0; ix < PoseCount; ++ix) {
                string pose = llList2String(Poses, ix);
                string prop = llList2String(Props, ix);
                llOwnerSay("| " + pose + " | " + prop + " | " + get_pose_data(pose));
            }
            llSetObjectName(name);
            dashes();
        } else if (llSubStringIndex(str, "REORIENT=") == 0) {
            // Reorient command (LINKMENU command from .MENUITEMS file)
            // str format: REORIENT=OFF=<x,y,z> or REORIENT=ROT=<x,y,z> (in degrees)
            list    parms = llParseString2List(str, ["="], []);
            vector  amount  = (vector)llList2String(parms, 2);
            llWhisper(0, "Adjusting props, please wait");
            
            if (llList2String(parms, 1) == "OFF") {
                adjust_all(TRUE, amount);
            } else {
                adjust_all(FALSE, amount);
            }
            llMessageLinked(LINK_THIS, 0, "AGAIN", (key)"");
            llWhisper(0, "Prop adjustment complete");
        } else if (str == "SAVEPROP") {
            llSay(ChatChan, "SAVE");
        }
    }

    timer() {
        llSay(ChatChan, "LIVE");
    }
        
}