]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/func/pointparticles.qc
#define use use1
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / func / pointparticles.qc
1 REGISTER_NET_LINKED(ENT_CLIENT_POINTPARTICLES)
2
3 #ifdef SVQC
4 // NOTE: also contains func_sparks
5
6 bool pointparticles_SendEntity(entity this, entity to, float fl)
7 {
8         WriteHeader(MSG_ENTITY, ENT_CLIENT_POINTPARTICLES);
9
10         // optional features to save space
11         fl = fl & 0x0F;
12         if(self.spawnflags & 2)
13                 fl |= 0x10; // absolute count on toggle-on
14         if(self.movedir != '0 0 0' || self.velocity != '0 0 0')
15                 fl |= 0x20; // 4 bytes - saves CPU
16         if(self.waterlevel || self.count != 1)
17                 fl |= 0x40; // 4 bytes - obscure features almost never used
18         if(self.mins != '0 0 0' || self.maxs != '0 0 0')
19                 fl |= 0x80; // 14 bytes - saves lots of space
20
21         WriteByte(MSG_ENTITY, fl);
22         if(fl & 2)
23         {
24                 if(self.state)
25                         WriteCoord(MSG_ENTITY, self.impulse);
26                 else
27                         WriteCoord(MSG_ENTITY, 0); // off
28         }
29         if(fl & 4)
30         {
31                 WriteCoord(MSG_ENTITY, self.origin_x);
32                 WriteCoord(MSG_ENTITY, self.origin_y);
33                 WriteCoord(MSG_ENTITY, self.origin_z);
34         }
35         if(fl & 1)
36         {
37                 if(self.model != "null")
38                 {
39                         WriteShort(MSG_ENTITY, self.modelindex);
40                         if(fl & 0x80)
41                         {
42                                 WriteCoord(MSG_ENTITY, self.mins_x);
43                                 WriteCoord(MSG_ENTITY, self.mins_y);
44                                 WriteCoord(MSG_ENTITY, self.mins_z);
45                                 WriteCoord(MSG_ENTITY, self.maxs_x);
46                                 WriteCoord(MSG_ENTITY, self.maxs_y);
47                                 WriteCoord(MSG_ENTITY, self.maxs_z);
48                         }
49                 }
50                 else
51                 {
52                         WriteShort(MSG_ENTITY, 0);
53                         if(fl & 0x80)
54                         {
55                                 WriteCoord(MSG_ENTITY, self.maxs_x);
56                                 WriteCoord(MSG_ENTITY, self.maxs_y);
57                                 WriteCoord(MSG_ENTITY, self.maxs_z);
58                         }
59                 }
60                 WriteShort(MSG_ENTITY, self.cnt);
61                 WriteString(MSG_ENTITY, self.mdl);
62                 if(fl & 0x20)
63                 {
64                         WriteShort(MSG_ENTITY, compressShortVector(self.velocity));
65                         WriteShort(MSG_ENTITY, compressShortVector(self.movedir));
66                 }
67                 if(fl & 0x40)
68                 {
69                         WriteShort(MSG_ENTITY, self.waterlevel * 16.0);
70                         WriteByte(MSG_ENTITY, self.count * 16.0);
71                 }
72                 WriteString(MSG_ENTITY, self.noise);
73                 if(self.noise != "")
74                 {
75                         WriteByte(MSG_ENTITY, floor(self.atten * 64));
76                         WriteByte(MSG_ENTITY, floor(self.volume * 255));
77                 }
78                 WriteString(MSG_ENTITY, self.bgmscript);
79                 if(self.bgmscript != "")
80                 {
81                         WriteByte(MSG_ENTITY, floor(self.bgmscriptattack * 64));
82                         WriteByte(MSG_ENTITY, floor(self.bgmscriptdecay * 64));
83                         WriteByte(MSG_ENTITY, floor(self.bgmscriptsustain * 255));
84                         WriteByte(MSG_ENTITY, floor(self.bgmscriptrelease * 64));
85                 }
86         }
87         return 1;
88 }
89
90 void pointparticles_use(entity this, entity actor, entity trigger)
91 {
92         this.state = !this.state;
93         this.SendFlags |= 2;
94 }
95
96 void pointparticles_think()
97 {SELFPARAM();
98         if(self.origin != self.oldorigin)
99         {
100                 self.SendFlags |= 4;
101                 self.oldorigin = self.origin;
102         }
103         self.nextthink = time;
104 }
105
106 void pointparticles_reset(entity this)
107 {
108         if(this.spawnflags & 1)
109                 this.state = 1;
110         else
111                 this.state = 0;
112 }
113
114 spawnfunc(func_pointparticles)
115 {
116         if(this.model != "") { precache_model(this.model); _setmodel(this, this.model); }
117         if(this.noise != "") precache_sound(this.noise);
118         if(this.mdl != "") this.cnt = 0; // use a good handler
119
120         if(!this.bgmscriptsustain) this.bgmscriptsustain = 1;
121         else if(this.bgmscriptsustain < 0) this.bgmscriptsustain = 0;
122
123         if(!this.atten) this.atten = ATTEN_NORM;
124         else if(this.atten < 0) this.atten = 0;
125         if(!this.volume) this.volume = 1;
126         if(!this.count) this.count = 1;
127         if(!this.impulse) this.impulse = 1;
128
129         if(!this.modelindex)
130         {
131                 setorigin(this, this.origin + this.mins);
132                 setsize(this, '0 0 0', this.maxs - this.mins);
133         }
134         //if(!this.cnt) this.cnt = _particleeffectnum(this.mdl);
135
136         Net_LinkEntity(this, (this.spawnflags & 4), 0, pointparticles_SendEntity);
137
138         IFTARGETED
139         {
140                 this.use = pointparticles_use;
141                 this.reset = pointparticles_reset;
142                 this.reset(this);
143         }
144         else
145                 this.state = 1;
146         this.think = pointparticles_think;
147         this.nextthink = time;
148 }
149
150 spawnfunc(func_sparks)
151 {
152         // self.cnt is the amount of sparks that one burst will spawn
153         if(self.cnt < 1) {
154                 self.cnt = 25.0; // nice default value
155         }
156
157         // self.wait is the probability that a sparkthink will spawn a spark shower
158         // range: 0 - 1, but 0 makes little sense, so...
159         if(self.wait < 0.05) {
160                 self.wait = 0.25; // nice default value
161         }
162
163         self.count = self.cnt;
164         self.mins = '0 0 0';
165         self.maxs = '0 0 0';
166         self.velocity = '0 0 -1';
167         self.mdl = "TE_SPARK";
168         self.impulse = 10 * self.wait; // by default 2.5/sec
169         self.wait = 0;
170         self.cnt = 0; // use mdl
171
172         spawnfunc_func_pointparticles(this);
173 }
174 #elif defined(CSQC)
175
176 .int dphitcontentsmask;
177
178 entityclass(PointParticles);
179 class(PointParticles) .int cnt; // effect number
180 class(PointParticles) .vector velocity; // particle velocity
181 class(PointParticles) .float waterlevel; // direction jitter
182 class(PointParticles) .int count; // count multiplier
183 class(PointParticles) .int impulse; // density
184 class(PointParticles) .string noise; // sound
185 class(PointParticles) .float atten;
186 class(PointParticles) .float volume;
187 class(PointParticles) .float absolute; // 1 = count per second is absolute, 2 = only spawn at toggle
188 class(PointParticles) .vector movedir; // trace direction
189 class(PointParticles) .float glow_color; // palette index
190
191 void Draw_PointParticles(entity this)
192 {
193         float n, i, fail;
194         vector p;
195         vector sz;
196         vector o;
197         o = self.origin;
198         sz = self.maxs - self.mins;
199         n = doBGMScript(self);
200         if(self.absolute == 2)
201         {
202                 if(n >= 0)
203                         n = self.just_toggled ? self.impulse : 0;
204                 else
205                         n = self.impulse * drawframetime;
206         }
207         else
208         {
209                 n *= self.impulse * drawframetime;
210                 if(self.just_toggled)
211                         if(n < 1)
212                                 n = 1;
213         }
214         if(n == 0)
215                 return;
216         fail = 0;
217         for(i = random(); i <= n && fail <= 64*n; ++i)
218         {
219                 p = o + self.mins;
220                 p.x += random() * sz.x;
221                 p.y += random() * sz.y;
222                 p.z += random() * sz.z;
223                 if(WarpZoneLib_BoxTouchesBrush(p, p, self, world))
224                 {
225                         if(self.movedir != '0 0 0')
226                         {
227                                 traceline(p, p + normalize(self.movedir) * 4096, 0, world);
228                                 p = trace_endpos;
229                                 int eff_num;
230                                 if(self.cnt)
231                                         eff_num = self.cnt;
232                                 else
233                                         eff_num = _particleeffectnum(self.mdl);
234                                 __pointparticles(eff_num, p, trace_plane_normal * vlen(self.movedir) + self.velocity + randomvec() * self.waterlevel, self.count);
235                         }
236                         else
237                         {
238                                 int eff_num;
239                                 if(self.cnt)
240                                         eff_num = self.cnt;
241                                 else
242                                         eff_num = _particleeffectnum(self.mdl);
243                                 __pointparticles(eff_num, p, self.velocity + randomvec() * self.waterlevel, self.count);
244                         }
245                         if(self.noise != "")
246                         {
247                                 setorigin(self, p);
248                                 _sound(self, CH_AMBIENT, self.noise, VOL_BASE * self.volume, self.atten);
249                         }
250                         self.just_toggled = 0;
251                 }
252                 else if(self.absolute)
253                 {
254                         ++fail;
255                         --i;
256                 }
257         }
258         setorigin(self, o);
259 }
260
261 void Ent_PointParticles_Remove(entity this)
262 {
263         if(this.noise)
264                 strunzone(this.noise);
265         this.noise = string_null;
266         if(this.bgmscript)
267                 strunzone(this.bgmscript);
268         this.bgmscript = string_null;
269         if(this.mdl)
270                 strunzone(this.mdl);
271         this.mdl = string_null;
272 }
273
274 NET_HANDLE(ENT_CLIENT_POINTPARTICLES, bool isnew)
275 {
276         float i;
277         vector v;
278         int f = ReadByte();
279         if(f & 2)
280         {
281                 i = ReadCoord(); // density (<0: point, >0: volume)
282                 if(i && !self.impulse && (self.cnt || self.mdl)) // self.cnt check is so it only happens if the ent already existed
283                         self.just_toggled = 1;
284                 self.impulse = i;
285         }
286         if(f & 4)
287         {
288                 self.origin_x = ReadCoord();
289                 self.origin_y = ReadCoord();
290                 self.origin_z = ReadCoord();
291         }
292         if(f & 1)
293         {
294                 self.modelindex = ReadShort();
295                 if(f & 0x80)
296                 {
297                         if(self.modelindex)
298                         {
299                                 self.mins_x = ReadCoord();
300                                 self.mins_y = ReadCoord();
301                                 self.mins_z = ReadCoord();
302                                 self.maxs_x = ReadCoord();
303                                 self.maxs_y = ReadCoord();
304                                 self.maxs_z = ReadCoord();
305                         }
306                         else
307                         {
308                                 self.mins    = '0 0 0';
309                                 self.maxs_x = ReadCoord();
310                                 self.maxs_y = ReadCoord();
311                                 self.maxs_z = ReadCoord();
312                         }
313                 }
314                 else
315                 {
316                         self.mins = self.maxs = '0 0 0';
317                 }
318
319                 self.cnt = ReadShort(); // effect number
320                 self.mdl = strzone(ReadString()); // effect string
321
322                 if(f & 0x20)
323                 {
324                         self.velocity = decompressShortVector(ReadShort());
325                         self.movedir = decompressShortVector(ReadShort());
326                 }
327                 else
328                 {
329                         self.velocity = self.movedir = '0 0 0';
330                 }
331                 if(f & 0x40)
332                 {
333                         self.waterlevel = ReadShort() / 16.0;
334                         self.count = ReadByte() / 16.0;
335                 }
336                 else
337                 {
338                         self.waterlevel = 0;
339                         self.count = 1;
340                 }
341                 if(self.noise)
342                         strunzone(self.noise);
343                 if(self.bgmscript)
344                         strunzone(self.bgmscript);
345                 self.noise = strzone(ReadString());
346                 if(self.noise != "")
347                 {
348                         self.atten = ReadByte() / 64.0;
349                         self.volume = ReadByte() / 255.0;
350                 }
351                 self.bgmscript = strzone(ReadString());
352                 if(self.bgmscript != "")
353                 {
354                         self.bgmscriptattack = ReadByte() / 64.0;
355                         self.bgmscriptdecay = ReadByte() / 64.0;
356                         self.bgmscriptsustain = ReadByte() / 255.0;
357                         self.bgmscriptrelease = ReadByte() / 64.0;
358                 }
359                 BGMScript_InitEntity(self);
360         }
361
362         return = true;
363
364         if(f & 2)
365         {
366                 self.absolute = (self.impulse >= 0);
367                 if(!self.absolute)
368                 {
369                         v = self.maxs - self.mins;
370                         self.impulse *= -v.x * v.y * v.z / 262144; // relative: particles per 64^3 cube
371                 }
372         }
373
374         if(f & 0x10)
375                 self.absolute = 2;
376
377         setorigin(self, self.origin);
378         setsize(self, self.mins, self.maxs);
379         self.solid = SOLID_NOT;
380         self.draw = Draw_PointParticles;
381         self.entremove = Ent_PointParticles_Remove;
382 }
383 #endif