]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/particles.qc
new smoke emitter for DF;
[divverent/nexuiz.git] / data / qcsrc / client / particles.qc
1 vector PointInBrush_vec;
2 entity PointInBrush_brush;
3 .float dphitcontentsmask;
4 float PointInBrush_Recurse()
5 {
6         float s;
7         entity se;
8         float f;
9
10         traceline(PointInBrush_vec, PointInBrush_vec, 0, world);
11         if not(trace_ent)
12                 return 0;
13         if(trace_ent == PointInBrush_brush)
14                 return 1;
15
16         se = trace_ent;
17         s = se.solid;
18         se.solid = SOLID_NOT;
19         f = PointInBrush_Recurse();
20         se.solid = s;
21
22         return f;
23 }
24 float PointInBrush(entity brush, vector point)
25 {
26         float f, s;
27
28         if not(brush.modelindex)
29                 return 1;
30
31         s = brush.solid;
32         brush.solid = SOLID_BSP;
33         PointInBrush_vec = point;
34         PointInBrush_brush = brush;
35         f = PointInBrush_Recurse();
36         brush.solid = s;
37
38         return f;
39 }
40
41 .float cnt; // effect number
42 .vector velocity; // particle velocity
43 .float waterlevel; // direction jitter
44 .float count; // count multiplier
45 .float impulse; // density
46 .string noise; // sound
47 .float atten;
48 .float volume;
49 .float absolute; // 1 = count per second is absolute, 2 = only spawn at toggle
50 .vector movedir; // trace direction
51 .string bgmscript;
52 .float bgmscriptdecay;
53
54 float pointparticles_scriptbuf;
55 float pointparticles_scriptbufsize;
56 float pointparticles_scriptbufloaded;
57 void PointparticlesScript_Init()
58 {
59         string s;
60         float fh;
61         pointparticles_scriptbuf = pointparticles_scriptbufsize = 0;
62         pointparticles_scriptbufloaded = 1;
63         s = strcat("maps/", mi_shortname, ".bgs");
64         fh = fopen(s, FILE_READ);
65         if(fh < 0)
66                 return;
67         pointparticles_scriptbuf = buf_create();
68         while((s = fgets(fh)))
69         {
70                 bufstr_set(pointparticles_scriptbuf, pointparticles_scriptbufsize, s);
71                 ++pointparticles_scriptbufsize;
72         }
73         fclose(fh);
74 }
75
76 .float scriptline;
77 .float scriptline0;
78 .float scriptvelocity;
79 .float scripttime;
80 .float scriptstate;
81 .float just_toggled;
82 void PointparticlesScript_InitEntity(entity e)
83 {
84         if(e.bgmscript != "")
85         {
86                 if(!pointparticles_scriptbufloaded)
87                         PointparticlesScript_Init();
88
89                 string mychar;
90                 float i;
91
92                 e.scriptline0 = -1;
93                 for(i = 0; i < pointparticles_scriptbufsize; ++i)
94                 {
95                         tokenize_sane(bufstr_get(pointparticles_scriptbuf, i));
96                         if(argv(0) == e.bgmscript)
97                                 break;
98                 }
99                 e.scriptline = e.scriptline0 = i;
100                 if(i >= pointparticles_scriptbufsize)
101                 {
102                         print("func_pointparticles: script does not define ", mychar, "\n");
103                         e.bgmscript = "";
104                 }
105         }
106 }
107
108 float PointparticlesScript(entity e)
109 {
110         float t;
111
112         if(e.bgmscript == "")
113                 return 1;
114         
115         if(cvar("bgmvolume") <= 0)
116                 return -1;
117
118         e.just_toggled = FALSE;
119
120         t = gettime(GETTIME_CDTRACK);
121
122         tokenize_sane(bufstr_get(pointparticles_scriptbuf, e.scriptline));
123         if(t < e.scripttime)
124         {
125                 e.scriptline = e.scriptline0;
126                 e.scripttime = 0;
127                 tokenize_sane(bufstr_get(pointparticles_scriptbuf, e.scriptline));
128         }
129
130         if(argv(0) != e.bgmscript)
131         {
132                 // end of script, will revert to beginning later
133         }
134         else if(t >= stof(argv(1)))
135         {
136                 // time code reached!
137                 e.scriptvelocity = stof(argv(2));
138                 if(e.scriptvelocity > 0)
139                         e.just_toggled = e.scriptstate = TRUE;
140                 else
141                         e.just_toggled = e.scriptstate = FALSE;
142                 e.scriptline += 1;
143                 e.scripttime = stof(argv(1));
144         }
145
146         if(e.scriptstate)
147                 return pow(0.5, (t - e.scripttime) * e.bgmscriptdecay) * e.scriptvelocity;
148         else
149                 return 0;
150 }
151
152 void Draw_PointParticles()
153 {
154         float n, i, fail;
155         vector p;
156         vector sz;
157         vector o;
158         o = self.origin;
159         sz = self.maxs - self.mins;
160         n = PointparticlesScript(self);
161         if(self.absolute == 2)
162         {
163                 if(n >= 0)
164                         n = self.just_toggled ? self.impulse : 0;
165                 else
166                         n = self.impulse * drawframetime;
167         }
168         else
169         {
170                 n *= self.impulse * drawframetime;
171                 if(self.just_toggled)
172                         if(n < 1)
173                                 n = 1;
174         }
175         if(n == 0)
176                 return;
177         fail = 0;
178         for(i = random(); i <= n && fail <= 64*n; ++i)
179         {
180                 p = o + self.mins;
181                 p_x += random() * sz_x;
182                 p_y += random() * sz_y;
183                 p_z += random() * sz_z;
184                 if(PointInBrush(self, p))
185                 {
186                         if(self.movedir != '0 0 0')
187                         {
188                                 traceline(p, p + normalize(self.movedir) * 4096, 0, world);
189                                 p = trace_endpos;
190                                 pointparticles(self.cnt, p, trace_plane_normal * vlen(self.movedir) + self.velocity + randomvec() * self.waterlevel, self.count);
191                         }
192                         else
193                         {
194                                 pointparticles(self.cnt, p, self.velocity + randomvec() * self.waterlevel, self.count);
195                         }
196                         if(self.noise != "")
197                         {
198                                 self.origin = p;
199                                 sound(self, CHAN_AUTO, self.noise, VOL_BASE * self.volume, self.atten);
200                         }
201                         self.just_toggled = 0;
202                 }
203                 else if(self.absolute)
204                 {
205                         ++fail;
206                         --i;
207                 }
208         }
209         self.origin = o;
210 }
211
212 void Ent_PointParticles_Remove()
213 {
214         if(self.noise)
215                 strunzone(self.noise);
216         self.noise = string_null;
217         if(self.bgmscript)
218                 strunzone(self.bgmscript);
219         self.bgmscript = string_null;
220 }
221
222 void Ent_PointParticles()
223 {
224         float f, i;
225         vector v;
226         f = ReadByte();
227         if(f & 2)
228         {
229                 i = ReadCoord(); // density (<0: point, >0: volume)
230                 if(i && !self.impulse && self.cnt) // self.cnt check is so it only happens if the ent already existed
231                         self.just_toggled = 1;
232                 self.impulse = i;
233         }
234         if(f & 4)
235         {
236                 self.origin_x = ReadCoord();
237                 self.origin_y = ReadCoord();
238                 self.origin_z = ReadCoord();
239         }
240         if(f & 1)
241         {
242                 self.modelindex = ReadShort();
243                 if(f & 0x80)
244                 {
245                         if(self.modelindex)
246                         {
247                                 self.mins_x = ReadCoord();
248                                 self.mins_y = ReadCoord();
249                                 self.mins_z = ReadCoord();
250                                 self.maxs_x = ReadCoord();
251                                 self.maxs_y = ReadCoord();
252                                 self.maxs_z = ReadCoord();
253                         }
254                         else
255                         {
256                                 self.mins    = '0 0 0';
257                                 self.maxs_x = ReadCoord();
258                                 self.maxs_y = ReadCoord();
259                                 self.maxs_z = ReadCoord();
260                         }
261                 }
262                 else
263                 {
264                         self.mins = self.maxs = '0 0 0';
265                 }
266
267                 self.cnt = ReadShort(); // effect number
268
269                 if(f & 0x20)
270                 {
271                         self.velocity = decompressShortVector(ReadShort());
272                         self.movedir = decompressShortVector(ReadShort());
273                 }
274                 else
275                 {
276                         self.velocity = self.movedir = '0 0 0';
277                 }
278                 if(f & 0x40)
279                 {
280                         self.waterlevel = ReadShort() / 16.0;
281                         self.count = ReadByte() / 16.0;
282                 }
283                 else
284                 {
285                         self.waterlevel = 0;
286                         self.count = 1;
287                 }
288                 if(self.noise)
289                         strunzone(self.noise);
290                 if(self.bgmscript)
291                         strunzone(self.bgmscript);
292                 self.noise = strzone(ReadString());
293                 if(self.noise != "")
294                 {
295                         self.atten = ReadByte() / 64.0;
296                         self.volume = ReadByte() / 255.0;
297                 }
298                 self.bgmscript = strzone(ReadString());
299                 if(self.bgmscript != "")
300                         self.bgmscriptdecay = ReadByte() / 255.0;
301                 PointparticlesScript_InitEntity(self);
302         }
303
304         if(f & 2)
305         {
306                 self.absolute = (self.impulse >= 0);
307                 if(!self.absolute)
308                 {
309                         v = self.maxs - self.mins;
310                         self.impulse *= -v_x * v_y * v_z / 262144; // relative: particles per 64^3 cube
311                 }
312         }
313
314         if(f & 0x10)
315                 self.absolute = 2;
316
317         setorigin(self, self.origin);
318         setsize(self, self.mins, self.maxs);
319         self.solid = SOLID_NOT;
320         self.draw = Draw_PointParticles;
321         self.entremove = Ent_PointParticles_Remove;
322 }
323
324 .float glow_color; // palette index
325 void Draw_Rain()
326 {
327     te_particlerain(self.origin + self.mins, self.origin + self.maxs, self.velocity, self.count * drawframetime, self.glow_color);
328 }
329
330 void Draw_Snow()
331 {
332     te_particlesnow(self.origin + self.mins, self.origin + self.maxs, self.velocity, self.count * drawframetime, self.glow_color);
333 }
334
335 void Ent_RainOrSnow()
336 {
337         self.impulse = ReadByte(); // Rain, Snow, or Whatever
338         self.origin_x = ReadCoord();
339         self.origin_y = ReadCoord();
340         self.origin_z = ReadCoord();
341         self.maxs_x = ReadCoord();
342         self.maxs_y = ReadCoord();
343         self.maxs_z = ReadCoord();
344         self.velocity = decompressShortVector(ReadShort());
345         self.count = ReadShort() * 10;
346         self.glow_color = ReadByte(); // color
347
348         self.mins    = -0.5 * self.maxs;
349         self.maxs    =  0.5 * self.maxs;
350         self.origin  = self.origin - self.mins;
351
352         setorigin(self, self.origin);
353         setsize(self, self.mins, self.maxs);
354         self.solid = SOLID_NOT;
355         if(self.impulse)
356                 self.draw = Draw_Rain;
357         else
358                 self.draw = Draw_Snow;
359 }
360
361 entity zcurve;
362 void zcurveparticles(float effectnum, vector start, vector end, float end_dz, float speed, float depth)
363 {
364         // end_dz:
365         //   IF IT WERE A STRAIGHT LINE, it'd end end_dz above end
366
367         vector mid;
368         mid = (start + end) * 0.5;
369
370         end_dz *= 0.25;
371         mid_z += end_dz;
372
373         --depth;
374         if(depth < 0 || normalize(mid - start) * normalize(end - start) > 0.999999)
375         // TODO make this a variable threshold
376         // currently: 0.081 degrees
377         // 0.99999 would be 0.256 degrees and is visible
378         {
379                 zcurve.velocity = speed * normalize(end - start);
380                 trailparticles(zcurve, effectnum, start, end);
381         }
382         else
383         {
384                 zcurveparticles(effectnum, start, mid, end_dz, speed, depth);
385                 zcurveparticles(effectnum, mid, end, end_dz, speed, depth);
386         }
387 }
388
389 void Net_ReadZCurveParticles()
390 {
391         vector start, end;
392         float end_dz;
393         float effectnum, speed;
394
395         if(!zcurve)
396         {
397                 zcurve = spawn();
398                 zcurve.classname = "zcurve";
399         }
400
401         effectnum = ReadShort();
402
403         start_x = ReadCoord();
404         start_y = ReadCoord();
405         start_z = ReadCoord();
406         end_x = ReadCoord();
407         end_y = ReadCoord();
408         end_z = ReadCoord();
409         end_dz = ReadCoord();
410         speed = ReadShort() * 16;
411
412         zcurveparticles(effectnum, start, end, end_dz, speed, 5); // at most 32 segments
413 }
414
415 void Net_ReadNexgunBeamParticle()
416 {
417         vector shotorg, endpos;
418         shotorg_x = ReadCoord(); shotorg_y = ReadCoord(); shotorg_z = ReadCoord();
419         endpos_x = ReadCoord(); endpos_y = ReadCoord(); endpos_z = ReadCoord();
420         
421         pointparticles(particleeffectnum("nex_muzzleflash"), shotorg, normalize(endpos - shotorg) * 1000, 1);
422         
423         //draw either the old v2.3 beam or the new beam
424         if (cvar("cl_particles_oldnexbeam") && (getstati(STAT_ALLOW_OLDNEXBEAM) || isdemo()))
425                 trailparticles(world, particleeffectnum("TE_TEI_G3"), shotorg, endpos);
426         else
427                 trailparticles(world, particleeffectnum("nex_beam"), shotorg, endpos);
428 }