]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/particles.qc
tiny particle emitter script change: bgmscript now supports not just states 0 or...
[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 scriptstate;
80 .float scriptvelocity;
81 .float scripttime;
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.scriptstate = !!e.scriptstate; // turns 0 to 0 and 2 to 1
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.scriptstate = 0;
127                 tokenize_sane(bufstr_get(pointparticles_scriptbuf, e.scriptline));
128         }
129
130         if(argv(0) != e.bgmscript)
131         {
132                 e.scriptstate = 0; // 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                 e.scriptstate = (e.scriptvelocity > 0) ? 2 : 0;
139                 e.scripttime = stof(argv(1));
140                 e.scriptline += 1;
141         }
142
143         if(e.scriptstate)
144         {
145                 if(e.bgmscriptdecay >= 1)
146                         return (e.scriptstate == 2);
147                 else
148                         return pow(0.5, (t - e.scripttime) * (e.bgmscriptdecay / (1 - e.bgmscriptdecay))) * e.scriptvelocity;
149         }
150         else
151                 return 0;
152 }
153
154 void Draw_PointParticles()
155 {
156         float n, i, fail, force;
157         vector p;
158         vector sz;
159         vector o;
160         o = self.origin;
161         sz = self.maxs - self.mins;
162         n = self.impulse * drawframetime;
163         n *= PointparticlesScript(self);
164         if(n == 0)
165                 return;
166         fail = 0;
167         force = (self.scriptstate == 2);
168         for(i = random(); (force || i <= n) && fail <= 64*n; ++i)
169         {
170                 p = o + self.mins;
171                 p_x += random() * sz_x;
172                 p_y += random() * sz_y;
173                 p_z += random() * sz_z;
174                 if(PointInBrush(self, p))
175                 {
176                         if(self.movedir != '0 0 0')
177                         {
178                                 traceline(p, p + normalize(self.movedir) * 4096, 0, world);
179                                 p = trace_endpos;
180                                 pointparticles(self.cnt, p, trace_plane_normal * vlen(self.movedir) + self.velocity + randomvec() * self.waterlevel, self.count, self.glow_color);
181                         }
182                         else
183                                 pointparticles(self.cnt, p, self.velocity + randomvec() * self.waterlevel, self.count, self.glow_color);
184                         if(self.noise != "")
185                         {
186                                 self.origin = p;
187                                 sound(self, CHAN_AUTO, self.noise, VOL_BASE * self.volume, self.atten);
188                         }
189                         force = 0;
190                 }
191                 else if(self.absolute)
192                 {
193                         ++fail;
194                         --i;
195                 }
196         }
197         self.origin = o;
198 }
199
200 void Ent_PointParticles_Remove()
201 {
202         if(self.noise)
203                 strunzone(self.noise);
204         self.noise = string_null;
205         if(self.bgmscript)
206                 strunzone(self.bgmscript);
207         self.bgmscript = string_null;
208 }
209
210 void Ent_PointParticles()
211 {
212         float f;
213         vector v;
214         f = ReadByte();
215         if(f & 2)
216         {
217                 self.impulse = ReadCoord(); // density (<0: point, >0: volume)
218         }
219         if(f & 4)
220         {
221                 self.origin_x = ReadCoord();
222                 self.origin_y = ReadCoord();
223                 self.origin_z = ReadCoord();
224         }
225         if(f & 1)
226         {
227                 self.modelindex = ReadShort();
228                 if(self.modelindex)
229                 {
230                         self.mins_x = ReadCoord();
231                         self.mins_y = ReadCoord();
232                         self.mins_z = ReadCoord();
233                         self.maxs_x = ReadCoord();
234                         self.maxs_y = ReadCoord();
235                         self.maxs_z = ReadCoord();
236                 }
237                 else
238                 {
239                         self.mins    = '0 0 0';
240                         self.maxs_x = ReadCoord();
241                         self.maxs_y = ReadCoord();
242                         self.maxs_z = ReadCoord();
243                 }
244
245                 self.cnt = ReadShort(); // effect number
246
247                 self.velocity = decompressShortVector(ReadShort());
248                 self.movedir = decompressShortVector(ReadShort());
249                 self.waterlevel = ReadCoord();
250                 self.count = ReadCoord();
251                 self.glow_color = ReadByte();
252                 if(self.noise)
253                         strunzone(self.noise);
254                 self.noise = strzone(ReadString());
255                 if(self.noise != "")
256                 {
257                         self.atten = ReadByte() / 64.0;
258                         self.volume = ReadByte() / 255.0;
259                 }
260                 if(self.bgmscript)
261                         strunzone(self.bgmscript);
262                 self.bgmscript = strzone(ReadString());
263                 if(self.bgmscript != "")
264                         self.bgmscriptdecay = ReadByte() / 255.0;
265                 PointparticlesScript_InitEntity(self);
266         }
267
268         if(f & 2)
269         {
270                 self.absolute = (self.impulse >= 0);
271                 if(!self.absolute)
272                 {
273                         v = self.maxs - self.mins;
274                         self.impulse *= -v_x * v_y * v_z / 262144; // relative: particles per 64^3 cube
275                 }
276         }
277
278         setorigin(self, self.origin);
279         setsize(self, self.mins, self.maxs);
280         self.solid = SOLID_NOT;
281         self.draw = Draw_PointParticles;
282         self.entremove = Ent_PointParticles_Remove;
283 }
284
285 void Draw_Rain()
286 {
287     te_particlerain(self.origin + self.mins, self.origin + self.maxs, self.velocity, self.count * drawframetime, self.glow_color);
288 }
289
290 void Draw_Snow()
291 {
292     te_particlesnow(self.origin + self.mins, self.origin + self.maxs, self.velocity, self.count * drawframetime, self.glow_color);
293 }
294
295 void Ent_RainOrSnow()
296 {
297         self.impulse = ReadByte(); // Rain, Snow, or Whatever
298         self.origin_x = ReadCoord();
299         self.origin_y = ReadCoord();
300         self.origin_z = ReadCoord();
301         self.maxs_x = ReadCoord();
302         self.maxs_y = ReadCoord();
303         self.maxs_z = ReadCoord();
304         self.velocity = decompressShortVector(ReadShort());
305         self.count = ReadShort() * 10;
306         self.glow_color = ReadByte(); // color
307
308         self.mins    = -0.5 * self.maxs;
309         self.maxs    =  0.5 * self.maxs;
310         self.origin  = self.origin - self.mins;
311
312         setorigin(self, self.origin);
313         setsize(self, self.mins, self.maxs);
314         self.solid = SOLID_NOT;
315         if(self.impulse)
316                 self.draw = Draw_Rain;
317         else
318                 self.draw = Draw_Snow;
319 }
320
321 entity zcurve;
322 void zcurveparticles(float effectnum, vector start, vector end, float end_dz, float speed, float depth)
323 {
324         // end_dz:
325         //   IF IT WERE A STRAIGHT LINE, it'd end end_dz above end
326
327         vector mid;
328         mid = (start + end) * 0.5;
329
330         end_dz *= 0.25;
331         mid_z += end_dz;
332
333         --depth;
334         if(depth < 0 || normalize(mid - start) * normalize(end - start) > 0.999999)
335         // TODO make this a variable threshold
336         // currently: 0.081 degrees
337         // 0.99999 would be 0.256 degrees and is visible
338         {
339                 zcurve.velocity = speed * normalize(end - start);
340                 trailparticles(zcurve, effectnum, start, end);
341         }
342         else
343         {
344                 zcurveparticles(effectnum, start, mid, end_dz, speed, depth);
345                 zcurveparticles(effectnum, mid, end, end_dz, speed, depth);
346         }
347 }
348
349 void Net_ReadZCurveParticles()
350 {
351         vector start, end;
352         float end_dz;
353         float effectnum, speed;
354
355         if(!zcurve)
356         {
357                 zcurve = spawn();
358                 zcurve.classname = "zcurve";
359         }
360
361         effectnum = ReadShort();
362
363         start_x = ReadCoord();
364         start_y = ReadCoord();
365         start_z = ReadCoord();
366         end_x = ReadCoord();
367         end_y = ReadCoord();
368         end_z = ReadCoord();
369         end_dz = ReadCoord();
370         speed = ReadShort() * 16;
371
372         zcurveparticles(effectnum, start, end, end_dz, speed, 5); // at most 32 segments
373 }
374
375 void Net_ReadNexgunBeamParticle()
376 {
377         vector shotorg, endpos;
378         shotorg_x = ReadCoord(); shotorg_y = ReadCoord(); shotorg_z = ReadCoord();
379         endpos_x = ReadCoord(); endpos_y = ReadCoord(); endpos_z = ReadCoord();
380         
381         pointparticles(particleeffectnum("nex_muzzleflash"), shotorg, normalize(endpos - shotorg) * 1000, 1);
382         
383         //draw either the old v2.3 beam or the new beam
384         if (cvar("cl_particles_oldnexbeam") && (getstati(STAT_ALLOW_OLDNEXBEAM) || isdemo()))
385                 trailparticles(world, particleeffectnum("TE_TEI_G3"), shotorg, endpos);
386         else
387                 trailparticles(world, particleeffectnum("nex_beam"), shotorg, endpos);
388 }