]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/particles.qc
more fixes for pointparticles bgm script
[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 glow_color; // palette color
46 .float impulse; // density
47 .string noise; // sound
48 .float atten;
49 .float volume;
50 .float absolute;
51 .vector movedir; // trace direction
52 .string bgmscript;
53 .float bgmscriptdecay;
54
55 float pointparticles_scriptbuf;
56 float pointparticles_scriptbufsize;
57 float pointparticles_scriptbufloaded;
58 void PointparticlesScript_Init()
59 {
60         string s;
61         float fh;
62         pointparticles_scriptbuf = pointparticles_scriptbufsize = 0;
63         pointparticles_scriptbufloaded = 1;
64         s = strcat("maps/", mi_shortname, ".bgs");
65         fh = fopen(s, FILE_READ);
66         if(fh < 0)
67                 return;
68         pointparticles_scriptbuf = buf_create();
69         while((s = fgets(fh)))
70         {
71                 bufstr_set(pointparticles_scriptbuf, pointparticles_scriptbufsize, s);
72                 ++pointparticles_scriptbufsize;
73         }
74         fclose(fh);
75 }
76
77 .float scriptline;
78 .float scriptline0;
79 .float scriptvelocity;
80 .float scripttime;
81 .float switchedon;
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.switchedon = 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                 {
140                         e.switchedon = TRUE;
141                         e.scripttime = stof(argv(1));
142                 }
143                 else
144                         e.scripttime = 0;
145                 e.scriptline += 1;
146         }
147
148         if(e.scripttime)
149         {
150                 if(e.bgmscriptdecay >= 1)
151                 {
152                         return e.switchedon;
153                 }
154                 else
155                 {
156                         return pow(0.5, (t - e.scripttime) * (e.bgmscriptdecay / (1 - e.bgmscriptdecay))) * e.scriptvelocity;
157                 }
158         }
159         else
160                 return 0;
161 }
162
163 void Draw_PointParticles()
164 {
165         float n, i, fail;
166         vector p;
167         vector sz;
168         vector o;
169         o = self.origin;
170         sz = self.maxs - self.mins;
171         n = self.impulse * drawframetime;
172         n *= PointparticlesScript(self);
173         if(n == 0)
174                 return;
175         fail = 0;
176         for(i = random(); (self.switchedon || i <= n) && fail <= 64*n; ++i)
177         {
178                 p = o + self.mins;
179                 p_x += random() * sz_x;
180                 p_y += random() * sz_y;
181                 p_z += random() * sz_z;
182                 if(PointInBrush(self, p))
183                 {
184                         if(self.movedir != '0 0 0')
185                         {
186                                 traceline(p, p + normalize(self.movedir) * 4096, 0, world);
187                                 p = trace_endpos;
188                                 pointparticles(self.cnt, p, trace_plane_normal * vlen(self.movedir) + self.velocity + randomvec() * self.waterlevel, self.count, self.glow_color);
189                         }
190                         else
191                                 pointparticles(self.cnt, p, self.velocity + randomvec() * self.waterlevel, self.count, self.glow_color);
192                         if(self.noise != "")
193                         {
194                                 self.origin = p;
195                                 sound(self, CHAN_AUTO, self.noise, VOL_BASE * self.volume, self.atten);
196                         }
197                         self.switchedon = 0;
198                 }
199                 else if(self.absolute)
200                 {
201                         ++fail;
202                         --i;
203                 }
204         }
205         self.origin = o;
206 }
207
208 void Ent_PointParticles_Remove()
209 {
210         if(self.noise)
211                 strunzone(self.noise);
212         self.noise = string_null;
213         if(self.bgmscript)
214                 strunzone(self.bgmscript);
215         self.bgmscript = string_null;
216 }
217
218 void Ent_PointParticles()
219 {
220         float f, i;
221         vector v;
222         f = ReadByte();
223         if(f & 2)
224         {
225                 i = ReadCoord(); // density (<0: point, >0: volume)
226                 if(i && !self.impulse)
227                         self.switchedon = 1;
228                 self.impulse = i;
229         }
230         if(f & 4)
231         {
232                 self.origin_x = ReadCoord();
233                 self.origin_y = ReadCoord();
234                 self.origin_z = ReadCoord();
235         }
236         if(f & 1)
237         {
238                 self.modelindex = ReadShort();
239                 if(f & 0x80)
240                 {
241                         if(self.modelindex)
242                         {
243                                 self.mins_x = ReadCoord();
244                                 self.mins_y = ReadCoord();
245                                 self.mins_z = ReadCoord();
246                                 self.maxs_x = ReadCoord();
247                                 self.maxs_y = ReadCoord();
248                                 self.maxs_z = ReadCoord();
249                         }
250                         else
251                         {
252                                 self.mins    = '0 0 0';
253                                 self.maxs_x = ReadCoord();
254                                 self.maxs_y = ReadCoord();
255                                 self.maxs_z = ReadCoord();
256                         }
257                 }
258                 else
259                 {
260                         self.mins = self.maxs = '0 0 0';
261                 }
262
263                 self.cnt = ReadShort(); // effect number
264
265                 if(f & 0x20)
266                 {
267                         self.velocity = decompressShortVector(ReadShort());
268                         self.movedir = decompressShortVector(ReadShort());
269                 }
270                 else
271                 {
272                         self.velocity = self.movedir = '0 0 0';
273                 }
274                 if(f & 0x40)
275                 {
276                         self.waterlevel = ReadShort() / 16.0;
277                         self.count = ReadByte() / 16.0;
278                         self.glow_color = ReadByte();
279                 }
280                 else
281                 {
282                         self.waterlevel = self.glow_color = 0;
283                         self.count = 1;
284                 }
285                 if(self.noise)
286                         strunzone(self.noise);
287                 if(self.bgmscript)
288                         strunzone(self.bgmscript);
289                 if(f & 0x10)
290                 {
291                         self.noise = strzone(ReadString());
292                         if(self.noise != "")
293                         {
294                                 self.atten = ReadByte() / 64.0;
295                                 self.volume = ReadByte() / 255.0;
296                         }
297                         self.bgmscript = strzone(ReadString());
298                         if(self.bgmscript != "")
299                                 self.bgmscriptdecay = ReadByte() / 255.0;
300                         PointparticlesScript_InitEntity(self);
301                 }
302                 else
303                 {
304                         self.noise = self.bgmscript = string_null;
305                 }
306         }
307
308         if(f & 2)
309         {
310                 self.absolute = (self.impulse >= 0);
311                 if(!self.absolute)
312                 {
313                         v = self.maxs - self.mins;
314                         self.impulse *= -v_x * v_y * v_z / 262144; // relative: particles per 64^3 cube
315                 }
316         }
317
318         setorigin(self, self.origin);
319         setsize(self, self.mins, self.maxs);
320         self.solid = SOLID_NOT;
321         self.draw = Draw_PointParticles;
322         self.entremove = Ent_PointParticles_Remove;
323 }
324
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 }