]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/particles.qc
and more... changed decay's meaning, updated docs
[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 0.5;
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                 n = self.just_toggled ? self.impulse : 0;
164         }
165         else
166         {
167                 n *= self.impulse * drawframetime;
168                 if(self.just_toggled)
169                         if(n < 1)
170                                 n = 1;
171         }
172         if(n == 0)
173                 return;
174         fail = 0;
175         for(i = random(); i <= n && fail <= 64*n; ++i)
176         {
177                 p = o + self.mins;
178                 p_x += random() * sz_x;
179                 p_y += random() * sz_y;
180                 p_z += random() * sz_z;
181                 if(PointInBrush(self, p))
182                 {
183                         if(self.movedir != '0 0 0')
184                         {
185                                 traceline(p, p + normalize(self.movedir) * 4096, 0, world);
186                                 p = trace_endpos;
187                                 pointparticles(self.cnt, p, trace_plane_normal * vlen(self.movedir) + self.velocity + randomvec() * self.waterlevel, self.count);
188                         }
189                         else
190                         {
191                                 pointparticles(self.cnt, p, self.velocity + randomvec() * self.waterlevel, self.count);
192                         }
193                         if(self.noise != "")
194                         {
195                                 self.origin = p;
196                                 sound(self, CHAN_AUTO, self.noise, VOL_BASE * self.volume, self.atten);
197                         }
198                         self.just_toggled = 0;
199                 }
200                 else if(self.absolute)
201                 {
202                         ++fail;
203                         --i;
204                 }
205         }
206         self.origin = o;
207 }
208
209 void Ent_PointParticles_Remove()
210 {
211         if(self.noise)
212                 strunzone(self.noise);
213         self.noise = string_null;
214         if(self.bgmscript)
215                 strunzone(self.bgmscript);
216         self.bgmscript = string_null;
217 }
218
219 void Ent_PointParticles()
220 {
221         float f, i;
222         vector v;
223         f = ReadByte();
224         if(f & 2)
225         {
226                 i = ReadCoord(); // density (<0: point, >0: volume)
227                 if(i && !self.impulse && self.cnt) // self.cnt check is so it only happens if the ent already existed
228                         self.just_toggled = 1;
229                 self.impulse = i;
230         }
231         if(f & 4)
232         {
233                 self.origin_x = ReadCoord();
234                 self.origin_y = ReadCoord();
235                 self.origin_z = ReadCoord();
236         }
237         if(f & 1)
238         {
239                 self.modelindex = ReadShort();
240                 if(f & 0x80)
241                 {
242                         if(self.modelindex)
243                         {
244                                 self.mins_x = ReadCoord();
245                                 self.mins_y = ReadCoord();
246                                 self.mins_z = ReadCoord();
247                                 self.maxs_x = ReadCoord();
248                                 self.maxs_y = ReadCoord();
249                                 self.maxs_z = ReadCoord();
250                         }
251                         else
252                         {
253                                 self.mins    = '0 0 0';
254                                 self.maxs_x = ReadCoord();
255                                 self.maxs_y = ReadCoord();
256                                 self.maxs_z = ReadCoord();
257                         }
258                 }
259                 else
260                 {
261                         self.mins = self.maxs = '0 0 0';
262                 }
263
264                 self.cnt = ReadShort(); // effect number
265
266                 if(f & 0x20)
267                 {
268                         self.velocity = decompressShortVector(ReadShort());
269                         self.movedir = decompressShortVector(ReadShort());
270                 }
271                 else
272                 {
273                         self.velocity = self.movedir = '0 0 0';
274                 }
275                 if(f & 0x40)
276                 {
277                         self.waterlevel = ReadShort() / 16.0;
278                         self.count = ReadByte() / 16.0;
279                 }
280                 else
281                 {
282                         self.waterlevel = 0;
283                         self.count = 1;
284                 }
285                 if(self.noise)
286                         strunzone(self.noise);
287                 if(self.bgmscript)
288                         strunzone(self.bgmscript);
289                 self.noise = strzone(ReadString());
290                 if(self.noise != "")
291                 {
292                         self.atten = ReadByte() / 64.0;
293                         self.volume = ReadByte() / 255.0;
294                 }
295                 self.bgmscript = strzone(ReadString());
296                 if(self.bgmscript != "")
297                         self.bgmscriptdecay = ReadByte() / 255.0;
298                 PointparticlesScript_InitEntity(self);
299         }
300
301         if(f & 2)
302         {
303                 self.absolute = (self.impulse >= 0);
304                 if(!self.absolute)
305                 {
306                         v = self.maxs - self.mins;
307                         self.impulse *= -v_x * v_y * v_z / 262144; // relative: particles per 64^3 cube
308                 }
309         }
310
311         if(f & 0x10)
312                 self.absolute = 2;
313
314         setorigin(self, self.origin);
315         setsize(self, self.mins, self.maxs);
316         self.solid = SOLID_NOT;
317         self.draw = Draw_PointParticles;
318         self.entremove = Ent_PointParticles_Remove;
319 }
320
321 .float glow_color; // palette index
322 void Draw_Rain()
323 {
324     te_particlerain(self.origin + self.mins, self.origin + self.maxs, self.velocity, self.count * drawframetime, self.glow_color);
325 }
326
327 void Draw_Snow()
328 {
329     te_particlesnow(self.origin + self.mins, self.origin + self.maxs, self.velocity, self.count * drawframetime, self.glow_color);
330 }
331
332 void Ent_RainOrSnow()
333 {
334         self.impulse = ReadByte(); // Rain, Snow, or Whatever
335         self.origin_x = ReadCoord();
336         self.origin_y = ReadCoord();
337         self.origin_z = ReadCoord();
338         self.maxs_x = ReadCoord();
339         self.maxs_y = ReadCoord();
340         self.maxs_z = ReadCoord();
341         self.velocity = decompressShortVector(ReadShort());
342         self.count = ReadShort() * 10;
343         self.glow_color = ReadByte(); // color
344
345         self.mins    = -0.5 * self.maxs;
346         self.maxs    =  0.5 * self.maxs;
347         self.origin  = self.origin - self.mins;
348
349         setorigin(self, self.origin);
350         setsize(self, self.mins, self.maxs);
351         self.solid = SOLID_NOT;
352         if(self.impulse)
353                 self.draw = Draw_Rain;
354         else
355                 self.draw = Draw_Snow;
356 }
357
358 entity zcurve;
359 void zcurveparticles(float effectnum, vector start, vector end, float end_dz, float speed, float depth)
360 {
361         // end_dz:
362         //   IF IT WERE A STRAIGHT LINE, it'd end end_dz above end
363
364         vector mid;
365         mid = (start + end) * 0.5;
366
367         end_dz *= 0.25;
368         mid_z += end_dz;
369
370         --depth;
371         if(depth < 0 || normalize(mid - start) * normalize(end - start) > 0.999999)
372         // TODO make this a variable threshold
373         // currently: 0.081 degrees
374         // 0.99999 would be 0.256 degrees and is visible
375         {
376                 zcurve.velocity = speed * normalize(end - start);
377                 trailparticles(zcurve, effectnum, start, end);
378         }
379         else
380         {
381                 zcurveparticles(effectnum, start, mid, end_dz, speed, depth);
382                 zcurveparticles(effectnum, mid, end, end_dz, speed, depth);
383         }
384 }
385
386 void Net_ReadZCurveParticles()
387 {
388         vector start, end;
389         float end_dz;
390         float effectnum, speed;
391
392         if(!zcurve)
393         {
394                 zcurve = spawn();
395                 zcurve.classname = "zcurve";
396         }
397
398         effectnum = ReadShort();
399
400         start_x = ReadCoord();
401         start_y = ReadCoord();
402         start_z = ReadCoord();
403         end_x = ReadCoord();
404         end_y = ReadCoord();
405         end_z = ReadCoord();
406         end_dz = ReadCoord();
407         speed = ReadShort() * 16;
408
409         zcurveparticles(effectnum, start, end, end_dz, speed, 5); // at most 32 segments
410 }
411
412 void Net_ReadNexgunBeamParticle()
413 {
414         vector shotorg, endpos;
415         shotorg_x = ReadCoord(); shotorg_y = ReadCoord(); shotorg_z = ReadCoord();
416         endpos_x = ReadCoord(); endpos_y = ReadCoord(); endpos_z = ReadCoord();
417         
418         pointparticles(particleeffectnum("nex_muzzleflash"), shotorg, normalize(endpos - shotorg) * 1000, 1);
419         
420         //draw either the old v2.3 beam or the new beam
421         if (cvar("cl_particles_oldnexbeam") && (getstati(STAT_ALLOW_OLDNEXBEAM) || isdemo()))
422                 trailparticles(world, particleeffectnum("TE_TEI_G3"), shotorg, endpos);
423         else
424                 trailparticles(world, particleeffectnum("nex_beam"), shotorg, endpos);
425 }