Merge remote branch 'refs/remotes/origin/fruitiex/racefixes'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / target_music.qc
1 .float lifetime;
2 // values:
3 //   volume
4 //   noise
5 //   targetname
6 //   lifetime
7 //   fade_time
8 //   fade_rate
9 // when triggered, the music is overridden for activator until lifetime (or forever, if lifetime is 0)
10 // when targetname is not set, THIS ONE is default
11 void target_music_sendto(float to, float is)
12 {
13         WriteByte(to, SVC_TEMPENTITY);
14         WriteByte(to, TE_CSQC_TARGET_MUSIC);
15         WriteShort(to, num_for_edict(self));
16         WriteByte(to, self.volume * 255.0 * is);
17         WriteByte(to, self.fade_time * 16.0);
18         WriteByte(to, self.fade_rate * 16.0);
19         WriteByte(to, self.lifetime);
20         WriteString(to, self.noise);
21 }
22 void target_music_reset()
23 {
24         if(self.targetname == "")
25                 target_music_sendto(MSG_ALL, 1);
26 }
27 void target_music_use()
28 {
29         if(!activator)
30                 return;
31         msg_entity = activator;
32         target_music_sendto(MSG_ONE, 1);
33 }
34 void spawnfunc_target_music()
35 {
36         self.use = target_music_use;
37         self.reset = target_music_reset;
38         if(!self.volume)
39                 self.volume = 1;
40         if(self.targetname == "")
41                 target_music_sendto(MSG_INIT, 1);
42         else
43                 target_music_sendto(MSG_INIT, 0);
44 }
45 void TargetMusic_RestoreGame()
46 {
47         for(self = world; (self = find(self, classname, "target_music")); )
48         {
49                 if(self.targetname == "")
50                         target_music_sendto(MSG_INIT, 1);
51                 else
52                         target_music_sendto(MSG_INIT, 0);
53         }
54 }
55 // values:
56 //   volume
57 //   noise
58 //   targetname
59 //   fade_time
60 // spawnflags:
61 //   1 = START_OFF
62 // when triggered, it is disabled/enabled for everyone
63 float trigger_music_SendEntity(entity to, float sf)
64 {
65         WriteByte(MSG_ENTITY, ENT_CLIENT_TRIGGER_MUSIC);
66         sf &~= 0x80;
67         if(self.cnt)
68                 sf |= 0x80;
69         WriteByte(MSG_ENTITY, sf);
70         if(sf & 4)
71         {
72                 WriteCoord(MSG_ENTITY, self.origin_x);
73                 WriteCoord(MSG_ENTITY, self.origin_y);
74                 WriteCoord(MSG_ENTITY, self.origin_z);
75         }
76         if(sf & 1)
77         {
78                 if(self.model != "null")
79                 {
80                         WriteShort(MSG_ENTITY, self.modelindex);
81                         WriteCoord(MSG_ENTITY, self.mins_x);
82                         WriteCoord(MSG_ENTITY, self.mins_y);
83                         WriteCoord(MSG_ENTITY, self.mins_z);
84                         WriteCoord(MSG_ENTITY, self.maxs_x);
85                         WriteCoord(MSG_ENTITY, self.maxs_y);
86                         WriteCoord(MSG_ENTITY, self.maxs_z);
87                 }
88                 else
89                 {
90                         WriteShort(MSG_ENTITY, 0);
91                         WriteCoord(MSG_ENTITY, self.maxs_x);
92                         WriteCoord(MSG_ENTITY, self.maxs_y);
93                         WriteCoord(MSG_ENTITY, self.maxs_z);
94                 }
95                 WriteByte(MSG_ENTITY, self.volume * 255.0);
96                 WriteByte(MSG_ENTITY, self.fade_time * 16.0);
97                 WriteByte(MSG_ENTITY, self.fade_rate * 16.0);
98                 WriteString(MSG_ENTITY, self.noise);
99         }
100         return 1;
101 }
102 void trigger_music_reset()
103 {
104         self.cnt = !(self.spawnflags & 1);
105         self.SendFlags |= 0x80;
106 }
107 void trigger_music_use()
108 {
109         self.cnt = !self.cnt;
110         self.SendFlags |= 0x80;
111 }
112 void spawnfunc_trigger_music()
113 {
114         if(self.model != "")
115                 setmodel(self, self.model);
116         if(!self.volume)
117                 self.volume = 1;
118         if(!self.modelindex)
119         {
120                 setorigin(self, self.origin + self.mins);
121                 setsize(self, '0 0 0', self.maxs - self.mins);
122         }
123         trigger_music_reset();
124
125         self.use = trigger_music_use;
126         self.reset = trigger_music_reset;
127
128         Net_LinkEntity(self, FALSE, 0, trigger_music_SendEntity);
129 }