]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/target/voicescript.qc
Merge branch 'master' into TimePath/unified_weapons
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / target / voicescript.qc
1 #ifdef SVQC
2 .entity voicescript; // attached voice script
3 .float voicescript_index; // index of next voice, or -1 to use the randomized ones
4 .float voicescript_nextthink; // time to play next voice
5 .float voicescript_voiceend; // time when this voice ends
6
7 void target_voicescript_clear(entity pl)
8 {
9         pl.voicescript = world;
10 }
11
12 void target_voicescript_use()
13 {SELFPARAM();
14         if(activator.voicescript != self)
15         {
16                 activator.voicescript = self;
17                 activator.voicescript_index = 0;
18                 activator.voicescript_nextthink = time + self.delay;
19         }
20 }
21
22 void target_voicescript_next(entity pl)
23 {
24         entity vs;
25         float i, n, dt;
26
27         vs = pl.voicescript;
28         if(!vs)
29                 return;
30         if(vs.message == "")
31                 return;
32         if (!IS_PLAYER(pl))
33                 return;
34         if(gameover)
35                 return;
36
37         if(time >= pl.voicescript_voiceend)
38         {
39                 if(time >= pl.voicescript_nextthink)
40                 {
41                         // get the next voice...
42                         n = tokenize_console(vs.message);
43
44                         if(pl.voicescript_index < vs.cnt)
45                                 i = pl.voicescript_index * 2;
46                         else if(n > vs.cnt * 2)
47                                 i = ((pl.voicescript_index - vs.cnt) % ((n - vs.cnt * 2 - 1) / 2)) * 2 + vs.cnt * 2 + 1;
48                         else
49                                 i = -1;
50
51                         if(i >= 0)
52                         {
53                                 play2(pl, strcat(vs.netname, "/", argv(i), ".wav"));
54                                 dt = stof(argv(i + 1));
55                                 if(dt >= 0)
56                                 {
57                                         pl.voicescript_voiceend = time + dt;
58                                         pl.voicescript_nextthink = pl.voicescript_voiceend + vs.wait * (0.5 + random());
59                                 }
60                                 else
61                                 {
62                                         pl.voicescript_voiceend = time - dt;
63                                         pl.voicescript_nextthink = pl.voicescript_voiceend;
64                                 }
65
66                                 pl.voicescript_index += 1;
67                         }
68                         else
69                         {
70                                 pl.voicescript = world; // stop trying then
71                         }
72                 }
73         }
74 }
75
76 spawnfunc(target_voicescript)
77 {
78         // netname: directory of the sound files
79         // message: list of "sound file" duration "sound file" duration, a *, and again a list
80         //          foo1 4.1 foo2 4.0 foo3 -3.1 * fool1 1.1 fool2 7.1 fool3 9.1 fool4 3.7
81         //          Here, a - in front of the duration means that no delay is to be
82         //          added after this message
83         // wait: average time between messages
84         // delay: initial delay before the first message
85
86         float i, n;
87         self.use = target_voicescript_use;
88
89         n = tokenize_console(self.message);
90         self.cnt = n / 2;
91         for(i = 0; i+1 < n; i += 2)
92         {
93                 if(argv(i) == "*")
94                 {
95                         self.cnt = i / 2;
96                         ++i;
97                 }
98                 precache_sound(strcat(self.netname, "/", argv(i), ".wav"));
99         }
100 }
101 #endif