]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/particles.qc
Only interpolate when not local beam
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / particles.qc
1 .float dphitcontentsmask;
2
3 .float cnt; // effect number
4 .vector velocity; // particle velocity
5 .float waterlevel; // direction jitter
6 .float count; // count multiplier
7 .float impulse; // density
8 .string noise; // sound
9 .float atten;
10 .float volume;
11 .float absolute; // 1 = count per second is absolute, 2 = only spawn at toggle
12 .vector movedir; // trace direction
13
14 void Draw_PointParticles()
15 {
16         float n, i, fail;
17         vector p;
18         vector sz;
19         vector o;
20         o = self.origin;
21         sz = self.maxs - self.mins;
22         n = BGMScript(self);
23         if(self.absolute == 2)
24         {
25                 if(n >= 0)
26                         n = self.just_toggled ? self.impulse : 0;
27                 else
28                         n = self.impulse * drawframetime;
29         }
30         else
31         {
32                 n *= self.impulse * drawframetime;
33                 if(self.just_toggled)
34                         if(n < 1)
35                                 n = 1;
36         }
37         if(n == 0)
38                 return;
39         fail = 0;
40         for(i = random(); i <= n && fail <= 64*n; ++i)
41         {
42                 p = o + self.mins;
43                 p_x += random() * sz_x;
44                 p_y += random() * sz_y;
45                 p_z += random() * sz_z;
46                 if(WarpZoneLib_BoxTouchesBrush(p, p, self, world))
47                 {
48                         if(self.movedir != '0 0 0')
49                         {
50                                 traceline(p, p + normalize(self.movedir) * 4096, 0, world);
51                                 p = trace_endpos;
52                                 pointparticles(self.cnt, p, trace_plane_normal * vlen(self.movedir) + self.velocity + randomvec() * self.waterlevel, self.count);
53                         }
54                         else
55                         {
56                                 pointparticles(self.cnt, p, self.velocity + randomvec() * self.waterlevel, self.count);
57                         }
58                         if(self.noise != "")
59                         {
60                                 setorigin(self, p);
61                                 sound(self, CH_AMBIENT, self.noise, VOL_BASE * self.volume, self.atten);
62                         }
63                         self.just_toggled = 0;
64                 }
65                 else if(self.absolute)
66                 {
67                         ++fail;
68                         --i;
69                 }
70         }
71         setorigin(self, o);
72 }
73
74 void Ent_PointParticles_Remove()
75 {
76         if(self.noise)
77                 strunzone(self.noise);
78         self.noise = string_null;
79         if(self.bgmscript)
80                 strunzone(self.bgmscript);
81         self.bgmscript = string_null;
82 }
83
84 void Ent_PointParticles()
85 {
86         float f, i;
87         vector v;
88         f = ReadByte();
89         if(f & 2)
90         {
91                 i = ReadCoord(); // density (<0: point, >0: volume)
92                 if(i && !self.impulse && self.cnt) // self.cnt check is so it only happens if the ent already existed
93                         self.just_toggled = 1;
94                 self.impulse = i;
95         }
96         if(f & 4)
97         {
98                 self.origin_x = ReadCoord();
99                 self.origin_y = ReadCoord();
100                 self.origin_z = ReadCoord();
101         }
102         if(f & 1)
103         {
104                 self.modelindex = ReadShort();
105                 if(f & 0x80)
106                 {
107                         if(self.modelindex)
108                         {
109                                 self.mins_x = ReadCoord();
110                                 self.mins_y = ReadCoord();
111                                 self.mins_z = ReadCoord();
112                                 self.maxs_x = ReadCoord();
113                                 self.maxs_y = ReadCoord();
114                                 self.maxs_z = ReadCoord();
115                         }
116                         else
117                         {
118                                 self.mins    = '0 0 0';
119                                 self.maxs_x = ReadCoord();
120                                 self.maxs_y = ReadCoord();
121                                 self.maxs_z = ReadCoord();
122                         }
123                 }
124                 else
125                 {
126                         self.mins = self.maxs = '0 0 0';
127                 }
128
129                 self.cnt = ReadShort(); // effect number
130
131                 if(f & 0x20)
132                 {
133                         self.velocity = decompressShortVector(ReadShort());
134                         self.movedir = decompressShortVector(ReadShort());
135                 }
136                 else
137                 {
138                         self.velocity = self.movedir = '0 0 0';
139                 }
140                 if(f & 0x40)
141                 {
142                         self.waterlevel = ReadShort() / 16.0;
143                         self.count = ReadByte() / 16.0;
144                 }
145                 else
146                 {
147                         self.waterlevel = 0;
148                         self.count = 1;
149                 }
150                 if(self.noise)
151                         strunzone(self.noise);
152                 if(self.bgmscript)
153                         strunzone(self.bgmscript);
154                 self.noise = strzone(ReadString());
155                 if(self.noise != "")
156                 {
157                         self.atten = ReadByte() / 64.0;
158                         self.volume = ReadByte() / 255.0;
159                 }
160                 self.bgmscript = strzone(ReadString());
161                 if(self.bgmscript != "")
162                 {
163                         self.bgmscriptattack = ReadByte() / 64.0;
164                         self.bgmscriptdecay = ReadByte() / 64.0;
165                         self.bgmscriptsustain = ReadByte() / 255.0;
166                         self.bgmscriptrelease = ReadByte() / 64.0;
167                 }
168                 BGMScript_InitEntity(self);
169         }
170
171         if(f & 2)
172         {
173                 self.absolute = (self.impulse >= 0);
174                 if(!self.absolute)
175                 {
176                         v = self.maxs - self.mins;
177                         self.impulse *= -v_x * v_y * v_z / 262144; // relative: particles per 64^3 cube
178                 }
179         }
180
181         if(f & 0x10)
182                 self.absolute = 2;
183
184         setorigin(self, self.origin);
185         setsize(self, self.mins, self.maxs);
186         self.solid = SOLID_NOT;
187         self.draw = Draw_PointParticles;
188         self.entremove = Ent_PointParticles_Remove;
189 }
190
191 .float glow_color; // palette index
192 void Draw_Rain()
193 {
194     te_particlerain(self.origin + self.mins, self.origin + self.maxs, self.velocity, floor(self.count * drawframetime + random()), self.glow_color);
195 }
196
197 void Draw_Snow()
198 {
199     te_particlesnow(self.origin + self.mins, self.origin + self.maxs, self.velocity, floor(self.count * drawframetime + random()), self.glow_color);
200 }
201
202 void Ent_RainOrSnow()
203 {
204         self.impulse = ReadByte(); // Rain, Snow, or Whatever
205         self.origin_x = ReadCoord();
206         self.origin_y = ReadCoord();
207         self.origin_z = ReadCoord();
208         self.maxs_x = ReadCoord();
209         self.maxs_y = ReadCoord();
210         self.maxs_z = ReadCoord();
211         self.velocity = decompressShortVector(ReadShort());
212         self.count = ReadShort() * 10;
213         self.glow_color = ReadByte(); // color
214
215         self.mins    = -0.5 * self.maxs;
216         self.maxs    =  0.5 * self.maxs;
217         self.origin  = self.origin - self.mins;
218
219         setorigin(self, self.origin);
220         setsize(self, self.mins, self.maxs);
221         self.solid = SOLID_NOT;
222         if(self.impulse)
223                 self.draw = Draw_Rain;
224         else
225                 self.draw = Draw_Snow;
226 }
227
228 void Net_ReadVortexBeamParticle()
229 {
230         vector shotorg, endpos;
231         float charge;
232         shotorg_x = ReadCoord(); shotorg_y = ReadCoord(); shotorg_z = ReadCoord();
233         endpos_x = ReadCoord(); endpos_y = ReadCoord(); endpos_z = ReadCoord();
234         charge = ReadByte() / 255.0;
235
236         pointparticles(particleeffectnum("nex_muzzleflash"), shotorg, normalize(endpos - shotorg) * 1000, 1);
237
238         //draw either the old v2.3 beam or the new beam
239         charge = sqrt(charge); // divide evenly among trail spacing and alpha
240         particles_alphamin = particles_alphamax = particles_fade = charge;
241
242         if (autocvar_cl_particles_oldnexbeam && (getstati(STAT_ALLOW_OLDNEXBEAM) || isdemo()))
243                 WarpZone_TrailParticles_WithMultiplier(world, particleeffectnum("TE_TEI_G3"), shotorg, endpos, 1, PARTICLES_USEALPHA | PARTICLES_USEFADE);
244         else
245                 WarpZone_TrailParticles_WithMultiplier(world, particleeffectnum("nex_beam"), shotorg, endpos, 1, PARTICLES_USEALPHA | PARTICLES_USEFADE);
246 }
247
248 .vector sw_shotorg;
249 .vector sw_endpos;
250 .float sw_spread_max;
251 .float sw_spread_min;
252 .float sw_time;
253
254 void Draw_Shockwave()
255 {
256         float a = bound(0, (0.5 - ((time - self.sw_time) / 0.4)), 0.5);
257
258         if(!a) { remove(self); }
259         
260         vector deviation, angle;
261
262         vector sw_color = getcsqcplayercolor(self.sv_entnum); // GetTeamRGB(GetPlayerColor(self.sv_entnum));
263
264         vector first_min_end = '0 0 0', prev_min_end = '0 0 0', new_min_end = '0 0 0';
265         vector first_max_end = '0 0 0', prev_max_end = '0 0 0', new_max_end = '0 0 0';
266
267         float new_max_dist, new_min_dist;
268         
269         vector shotdir = normalize(self.sw_endpos - self.sw_shotorg);
270         vectorvectors(shotdir);
271         vector right = v_right;
272         vector up = v_up;
273         
274         float counter, dist_before_normal = 200, shots = 20;
275         
276         vector min_end = ((self.sw_shotorg + (shotdir * dist_before_normal)) + (up * self.sw_spread_min));
277         vector max_end = (self.sw_endpos + (up * self.sw_spread_max));
278         
279         float spread_to_min = vlen(normalize(min_end - self.sw_shotorg) - shotdir);
280         float spread_to_max = vlen(normalize(max_end - min_end) - shotdir);
281         
282         for(counter = 0; counter < shots; ++counter)
283         {
284                 // perfect circle effect lines
285                 angle = '0 0 0';
286                 makevectors('0 360 0' * (0.75 + (counter - 0.5) / shots));
287                 angle_y = v_forward_x;
288                 angle_z = v_forward_y;
289
290                 // first do the spread_to_min effect
291                 deviation = angle * spread_to_min;
292                 deviation = ((shotdir + (right * deviation_y) + (up * deviation_z)));
293                 new_min_dist = dist_before_normal;
294                 new_min_end = (self.sw_shotorg + (deviation * new_min_dist));
295                 //te_lightning2(world, new_min_end, self.sw_shotorg);
296
297                 // then calculate spread_to_max effect
298                 deviation = angle * spread_to_max;
299                 deviation = ((shotdir + (right * deviation_y) + (up * deviation_z)));
300                 new_max_dist = vlen(new_min_end - self.sw_endpos);
301                 new_max_end = (new_min_end + (deviation * new_max_dist));
302                 //te_lightning2(world, new_end, prev_min_end);
303                 
304
305                 if(counter == 0)
306                 {
307                         first_min_end = new_min_end;
308                         first_max_end = new_max_end;
309                 }
310
311                 if(counter >= 1)
312                 {
313                         R_BeginPolygon("", DRAWFLAG_NORMAL);
314                         R_PolygonVertex(prev_min_end, '0 0 0', sw_color, a);
315                         R_PolygonVertex(new_min_end, '0 0 0', sw_color, a);
316                         R_PolygonVertex(self.sw_shotorg, '0 0 0', sw_color, a);
317                         R_EndPolygon();
318
319                         R_BeginPolygon("", DRAWFLAG_NORMAL);
320                         R_PolygonVertex(new_min_end, '0 0 0', sw_color, a);
321                         R_PolygonVertex(prev_min_end, '0 0 0', sw_color, a);
322                         R_PolygonVertex(prev_max_end, '0 0 0', sw_color, a);
323                         R_PolygonVertex(new_max_end, '0 0 0', sw_color, a);
324                         R_EndPolygon();
325                 }
326
327                 prev_min_end = new_min_end;
328                 prev_max_end = new_max_end;
329
330                 if((counter + 1) == shots)
331                 {
332                         R_BeginPolygon("", DRAWFLAG_NORMAL);
333                         R_PolygonVertex(prev_min_end, '0 0 0', sw_color, a);
334                         R_PolygonVertex(first_min_end, '0 0 0', sw_color, a);
335                         R_PolygonVertex(self.sw_shotorg, '0 0 0', sw_color, a);
336                         R_EndPolygon();
337
338                         R_BeginPolygon("", DRAWFLAG_NORMAL);
339                         R_PolygonVertex(first_min_end, '0 0 0', sw_color, a);
340                         R_PolygonVertex(prev_min_end, '0 0 0', sw_color, a);
341                         R_PolygonVertex(prev_max_end, '0 0 0', sw_color, a);
342                         R_PolygonVertex(first_max_end, '0 0 0', sw_color, a);
343                         R_EndPolygon();
344                 }
345         }
346 }
347
348 void Net_ReadShockwaveParticle()
349 {
350         entity shockwave;
351         shockwave = spawn();
352         shockwave.draw = Draw_Shockwave;
353         
354         shockwave.sw_shotorg_x = ReadCoord(); shockwave.sw_shotorg_y = ReadCoord(); shockwave.sw_shotorg_z = ReadCoord();
355         shockwave.sw_endpos_x  = ReadCoord(); shockwave.sw_endpos_y  = ReadCoord(); shockwave.sw_endpos_z  = ReadCoord();
356         
357         shockwave.sw_spread_max = ReadByte();
358         shockwave.sw_spread_min = ReadByte();
359
360         shockwave.sv_entnum = ReadByte();
361
362         shockwave.sw_time = time;
363 }
364
365 .vector beam_color;
366 .float beam_alpha;
367 .float beam_thickness;
368 .float beam_traileffect;
369 .float beam_hiteffect;
370 .float beam_hitlight[4]; // 0: radius, 123: rgb
371 .float beam_muzzleeffect;
372 .float beam_muzzlelight[4]; // 0: radius, 123: rgb
373 .string beam_image;
374
375 .entity beam_muzzleentity;
376
377 .float beam_usevieworigin;
378 .float beam_initialized;
379 .float beam_maxangle;
380 .float beam_range;
381 .float beam_returnspeed;
382 .float beam_tightness;
383 .vector beam_shotorigin;
384 .vector beam_dir;
385
386 entity Draw_ArcBeam_callback_entity;
387 vector Draw_ArcBeam_callback_new_dir;
388 float Draw_ArcBeam_callback_segmentdist;
389 float Draw_ArcBeam_callback_last_thickness;
390 vector Draw_ArcBeam_callback_last_top;
391 vector Draw_ArcBeam_callback_last_bottom;
392
393 void Draw_ArcBeam_callback(vector start, vector hit, vector end)
394 {
395         entity beam = Draw_ArcBeam_callback_entity;
396         vector transformed_view_org;
397         transformed_view_org = WarpZone_TransformOrigin(WarpZone_trace_transform, view_origin);
398
399         vector thickdir = normalize(cross(normalize(start - hit), transformed_view_org - start));
400
401         vector hitorigin;
402
403         // draw segment
404         #if 0
405         if(trace_fraction != 1)
406         {
407                 // calculate our own hit origin as trace_endpos tends to jump around annoyingly (to player origin?)
408                 hitorigin = start + (Draw_ArcBeam_callback_new_dir * Draw_ArcBeam_callback_segmentdist * trace_fraction);
409                 hitorigin = WarpZone_TransformOrigin(WarpZone_trace_transform, hitorigin);
410         }
411         else
412         {
413                 hitorigin = hit;
414         }
415         #else
416         hitorigin = hit;
417         #endif
418
419         // decide upon thickness
420         float thickness = beam.beam_thickness;
421
422         // draw primary beam render
423         vector top    = hitorigin + (thickdir * thickness);
424         vector bottom = hitorigin - (thickdir * thickness);
425         vector last_top    = start + (thickdir * Draw_ArcBeam_callback_last_thickness);
426         vector last_bottom = start - (thickdir * Draw_ArcBeam_callback_last_thickness);
427
428         R_BeginPolygon(beam.beam_image, DRAWFLAG_NORMAL); // DRAWFLAG_ADDITIVE
429         R_PolygonVertex(
430                 top,
431                 '0 0.5 0' + ('0 0.5 0' * (thickness / beam.beam_thickness)),
432                 beam.beam_color,
433                 beam.beam_alpha
434         );
435         R_PolygonVertex(
436                 last_top,
437                 '0 0.5 0' + ('0 0.5 0' * (Draw_ArcBeam_callback_last_thickness / beam.beam_thickness)),
438                 beam.beam_color,
439                 beam.beam_alpha
440         );
441         R_PolygonVertex(
442                 last_bottom,
443                 '0 0.5 0' * (1 - (Draw_ArcBeam_callback_last_thickness / beam.beam_thickness)),
444                 beam.beam_color,
445                 beam.beam_alpha
446         );
447         R_PolygonVertex(
448                 bottom,
449                 '0 0.5 0' * (1 - (thickness / beam.beam_thickness)),
450                 beam.beam_color,
451                 beam.beam_alpha
452         );
453         R_EndPolygon();
454
455         // draw trailing particles
456         // NOTES:
457         //  - Don't use spammy particle counts here, use a FEW small particles around the beam
458         //  - We're not using WarpZone_TrailParticles here because we will handle warpzones ourselves.
459         if(beam.beam_traileffect)
460         {
461                 trailparticles(beam, beam.beam_traileffect, start, hitorigin);
462         }
463
464         // set up for the next 
465         Draw_ArcBeam_callback_last_thickness = thickness;
466         Draw_ArcBeam_callback_last_top = top;
467         Draw_ArcBeam_callback_last_bottom = bottom;
468 }
469
470 void Draw_ArcBeam()
471 {
472         if(!self.beam_usevieworigin)
473         {
474                 InterpolateOrigin_Do();
475         }
476
477         // origin = beam starting origin
478         // v_angle = wanted/aim direction
479         // angles = current direction of beam
480
481         vector start_pos;
482         vector wantdir; //= view_forward;
483         vector beamdir; //= self.beam_dir;
484
485         float segments;
486         if(self.beam_usevieworigin)
487         {
488                 // WEAPONTODO:
489                 // Currently we have to replicate nearly the same method of figuring
490                 // out the shotdir that the server does... Ideally in the future we
491                 // should be able to acquire this from a generalized function built
492                 // into a weapon system for client code. 
493
494                 // find where we are aiming
495                 makevectors(view_angles);
496
497                 // decide upon start position
498                 if(self.beam_usevieworigin == 2)
499                         { start_pos = view_origin; }
500                 else
501                         { start_pos = self.origin; }
502
503                 // trace forward with an estimation
504                 WarpZone_TraceLine(start_pos, start_pos + view_forward * self.beam_range, MOVE_NOMONSTERS, self);
505
506                 // untransform in case our trace went through a warpzone
507                 vector vf, vr, vu;
508                 vf = view_forward;
509                 vr = view_right;
510                 vu = view_up;
511                 vector shothitpos = WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos); // warpzone support
512                 view_forward = vf;
513                 view_right = vr;
514                 view_up = vu;
515
516                 // un-adjust trueaim if shotend is too close
517                 if(vlen(shothitpos - view_origin) < g_trueaim_minrange)
518                         shothitpos = view_origin + (view_forward * g_trueaim_minrange);
519
520                 // move shot origin to the actual gun muzzle origin
521                 vector origin_offset = view_forward * self.beam_shotorigin_x + view_right * -self.beam_shotorigin_y + view_up * self.beam_shotorigin_z;
522                 start_pos = start_pos + origin_offset;
523
524                 // calculate the aim direction now
525                 wantdir = normalize(shothitpos - start_pos);
526
527                 if(!self.beam_initialized)
528                 {
529                         self.beam_dir = wantdir;
530                         self.beam_initialized = TRUE;
531                 }
532
533                 // WEAPONTODO: Calculate segments dyanmically similarly to the server code
534                 segments = 20;
535                 if(self.beam_dir != wantdir)
536                 {
537                         float angle = ceil(vlen(wantdir - self.beam_dir) * RAD2DEG);
538                         float anglelimit;
539                         if(angle && (angle > self.beam_maxangle))
540                         {
541                                 // if the angle is greater than maxangle, force the blendfactor to make this the maximum factor
542                                 anglelimit = min(self.beam_maxangle / angle, 1);
543                         }
544                         else
545                         {
546                                 // the radius is not too far yet, no worries :D
547                                 anglelimit = 1;
548                         }
549
550                         // calculate how much we're going to move the end of the beam to the want position
551                         float blendfactor = bound(0, anglelimit * (1 - (self.beam_returnspeed * frametime)), 1);
552                         self.beam_dir = normalize((wantdir * (1 - blendfactor)) + (self.beam_dir * blendfactor));
553
554                         // WEAPONTODO (server and client):
555                         // blendfactor never actually becomes 0 in this situation, which is a problem
556                         // regarding precision... this means that self.beam_dir and w_shotdir approach
557                         // eachother, however they never actually become the same value with this method.
558
559                         // Perhaps we should do some form of rounding/snapping?
560
561                         // printf("blendfactor = %f\n", blendfactor);
562
563                         #if 0
564                         // calculate how many segments are needed
565                         float max_allowed_segments;
566
567                         if(WEP_CVAR(arc, beam_distancepersegment))
568                                 max_allowed_segments = min(ARC_MAX_SEGMENTS, 1 + (vlen(w_shotdir / WEP_CVAR(arc, beam_distancepersegment))));
569                         else
570                                 max_allowed_segments = ARC_MAX_SEGMENTS;
571
572                         if(WEP_CVAR(arc, beam_degreespersegment))
573                         {
574                                 segments = min( max(1, ( min(angle, WEP_CVAR(arc, beam_maxangle)) / WEP_CVAR(arc, beam_degreespersegment) ) ), max_allowed_segments );
575                         }
576                         else
577                         {
578                                 segments = 1;
579                         }
580                         #endif
581                 }
582                 #if 0
583                 else
584                 {
585                         segments = 1;
586                 }
587                 #endif
588
589                 // set the beam direction which the rest of the code will refer to
590                 beamdir = self.beam_dir;
591
592                 // finally, set self.angles to the proper direction so that muzzle attachment points in proper direction
593                 self.angles = fixedvectoangles2(view_forward, view_up);
594         }
595         else
596         {
597                 // set the values from the provided info from the networked entity
598                 start_pos = self.origin;
599                 wantdir = self.v_angle;
600                 beamdir = self.angles;
601
602                 // WEAPONTODO: Calculate segments dyanmically similarly to the server code
603                 segments = 20;
604                 #if 0
605                 if(beamdir != wantdir)
606                 {
607                         // calculate how many segments are needed
608                         float max_allowed_segments;
609
610                         if(WEP_CVAR(arc, beam_distancepersegment))
611                                 max_allowed_segments = min(ARC_MAX_SEGMENTS, 1 + (vlen(w_shotdir / WEP_CVAR(arc, beam_distancepersegment))));
612                         else
613                                 max_allowed_segments = ARC_MAX_SEGMENTS;
614
615                         if(WEP_CVAR(arc, beam_degreespersegment))
616                         {
617                                 segments = min( max(1, ( min(angle, WEP_CVAR(arc, beam_maxangle)) / WEP_CVAR(arc, beam_degreespersegment) ) ), max_allowed_segments );
618                         }
619                         else
620                         {
621                                 segments = 1;
622                         }
623                 }
624                 else
625                 {
626                         segments = 1;
627                 }
628                 #endif
629         }
630
631         setorigin(self, start_pos);
632         self.beam_muzzleentity.angles_z = random() * 360; // WEAPONTODO: use avelocity instead?
633
634         vector beam_endpos_estimate = (start_pos + (beamdir * self.beam_range));
635
636         Draw_ArcBeam_callback_entity = self;
637         Draw_ArcBeam_callback_last_thickness = 0;
638         Draw_ArcBeam_callback_last_top = start_pos;
639         Draw_ArcBeam_callback_last_bottom = start_pos;
640
641         vector last_origin = start_pos;
642         //vector hitorigin = start_pos;
643
644         float i;
645         for(i = 1; i <= segments; ++i)
646         {
647                 // WEAPONTODO (server and client):
648                 // Segment blend and distance should probably really be calculated in a better way,
649                 // however I am not sure how to do it properly. There are a few things I have tried,
650                 // but most of them do not work properly due to my lack of understanding regarding
651                 // the mathematics behind them.
652
653                 // Ideally, we should calculate the positions along a perfect curve
654                 // between wantdir and self.beam_dir with an option for depth of arc
655
656                 // Another issue is that (on the client code) we must separate the
657                 // curve into multiple rendered curves when handling warpzones.
658                 
659                 // I can handle this by detecting it for each segment, however that
660                 // is a fairly inefficient method in comparison to having a curved line
661                 // drawing function similar to Draw_CylindricLine that accepts
662                 // top and bottom origins as input, this way there would be no
663                 // overlapping edges when connecting the curved pieces.
664
665                 // WEAPONTODO (client):
666                 // In order to do nice fading and pointing on the starting segment, we must always
667                 // have that drawn as a separate triangle... However, that is difficult to do when
668                 // keeping in mind the above problems and also optimizing the amount of segments
669                 // drawn on screen at any given time. (Automatic beam quality scaling, essentially)
670
671                 // calculate this on every segment to ensure that we always reach the full length of the attack
672                 float segmentblend = bound(0, (i/segments) + self.beam_tightness, 1);
673                 float segmentdist = vlen(beam_endpos_estimate - last_origin) * (i/segments);
674
675                 vector new_dir = normalize( (wantdir * (1 - segmentblend)) + (normalize(beam_endpos_estimate - last_origin) * segmentblend) );
676                 vector new_origin = last_origin + (new_dir * segmentdist);
677
678                 Draw_ArcBeam_callback_segmentdist = segmentdist;
679                 Draw_ArcBeam_callback_new_dir = new_dir;
680
681                 WarpZone_TraceBox_ThroughZone(
682                         last_origin,
683                         '0 0 0',
684                         '0 0 0',
685                         new_origin,
686                         MOVE_NORMAL,
687                         world,
688                         world,
689                         Draw_ArcBeam_callback
690                 );
691
692                 // check if we're going to proceed with drawing
693                 //if(trace_fraction != 1)
694                 //{
695                         // we're done with drawing this frame
696                         //last_origin = last_origin + (new_dir * segmentdist * trace_fraction);
697                         //hitorigin = last_origin + (new_dir * segmentdist * trace_fraction);
698                         //break;
699                 //}
700                 //else
701                 {
702                         // continue onto the next segment
703                         last_origin = WarpZone_TransformOrigin(WarpZone_trace_transform, new_origin);
704                         beam_endpos_estimate = WarpZone_TransformOrigin(WarpZone_trace_transform, beam_endpos_estimate);
705                 }
706         }
707
708         if(self.beam_hiteffect)
709         {
710                 pointparticles(self.beam_hiteffect, last_origin, beamdir * -1, frametime * 2);
711         }
712         if(self.beam_hitlight[0])
713         {
714                 adddynamiclight(last_origin, self.beam_hitlight[0], vec3(self.beam_hitlight[1], self.beam_hitlight[2], self.beam_hitlight[3]));
715         }
716         if(self.beam_muzzleeffect)
717         {
718                 pointparticles(self.beam_muzzleeffect, start_pos + wantdir * 20, wantdir * 1000, frametime * 0.1);
719         }
720         if(self.beam_muzzlelight[0])
721         {
722                 adddynamiclight(start_pos + wantdir * 20, self.beam_muzzlelight[0], vec3(self.beam_muzzlelight[1], self.beam_muzzlelight[2], self.beam_muzzlelight[3]));
723         }
724
725         // cleanup
726         Draw_ArcBeam_callback_entity = world;
727         Draw_ArcBeam_callback_new_dir = '0 0 0';
728         Draw_ArcBeam_callback_segmentdist = 0;
729         Draw_ArcBeam_callback_last_thickness = 0;
730         Draw_ArcBeam_callback_last_top = '0 0 0';
731         Draw_ArcBeam_callback_last_bottom = '0 0 0';
732 }
733
734 void Remove_ArcBeam(void)
735 {
736         remove(self.beam_muzzleentity);
737         sound(self, CH_SHOTS_SINGLE, "misc/null.wav", VOL_BASE, ATTEN_NORM);
738 }
739
740 void Ent_ReadArcBeam(float isnew)
741 {
742         float sf = ReadByte();
743         entity flash;
744
745         if(isnew)
746         {
747                 // calculate shot origin offset from gun alignment
748                 float gunalign = autocvar_cl_gunalign;
749                 if(gunalign != 1 && gunalign != 2 && gunalign != 4)
750                         gunalign = 3; // default value
751                 --gunalign;
752
753                 self.beam_shotorigin = arc_shotorigin[gunalign];
754
755                 // set other main attributes of the beam
756                 self.draw = Draw_ArcBeam;
757                 self.entremove = Remove_ArcBeam;
758                 sound(self, CH_SHOTS_SINGLE, "weapons/lgbeam_fly.wav", VOL_BASE, ATTEN_NORM);
759
760                 flash = spawn();
761                 flash.owner = self;
762                 flash.effects = EF_ADDITIVE | EF_FULLBRIGHT;
763                 flash.drawmask = MASK_NORMAL;
764                 flash.solid = SOLID_NOT;
765                 setattachment(flash, self, "");
766                 setorigin(flash, '0 0 0');
767
768                 self.beam_muzzleentity = flash;
769         }
770         else
771         {
772                 flash = self.beam_muzzleentity;
773         }
774
775         if(sf & 1) // settings information
776         {
777                 self.beam_maxangle = ReadShort();
778                 self.beam_range = ReadCoord();
779                 self.beam_returnspeed = ReadShort();
780                 self.beam_tightness = (ReadByte() / 10);
781
782                 if(ReadByte())
783                 {
784                         if(autocvar_chase_active)
785                                 { self.beam_usevieworigin = 1; }
786                         else // use view origin
787                                 { self.beam_usevieworigin = 2; }
788                 }
789                 else
790                 {
791                         self.beam_usevieworigin = 0;
792                 }
793         }
794
795         if(!self.beam_usevieworigin)
796         {
797                 // self.iflags = IFLAG_ORIGIN | IFLAG_ANGLES | IFLAG_V_ANGLE; // why doesn't this work?
798                 self.iflags = IFLAG_ORIGIN;
799
800                 InterpolateOrigin_Undo();
801         }
802
803         if(sf & 2) // starting location
804         {
805                 self.origin_x = ReadCoord();
806                 self.origin_y = ReadCoord();
807                 self.origin_z = ReadCoord();
808         }
809         else if(self.beam_usevieworigin) // infer the location from player location
810         {
811                 if(self.beam_usevieworigin == 2)
812                 {
813                         // use view origin
814                         self.origin = view_origin;
815                 }
816                 else
817                 {
818                         // use player origin so that third person display still works
819                         self.origin = getplayerorigin(player_localnum) + ('0 0 1' * getstati(STAT_VIEWHEIGHT));
820                 }
821         }
822
823         setorigin(self, self.origin);
824
825         if(sf & 4) // want/aim direction
826         {
827                 self.v_angle_x = ReadCoord();
828                 self.v_angle_y = ReadCoord();
829                 self.v_angle_z = ReadCoord();
830         }
831
832         if(sf & 8) // beam direction
833         {
834                 self.angles_x = ReadCoord();
835                 self.angles_y = ReadCoord();
836                 self.angles_z = ReadCoord();
837         }
838
839         if(sf & 16) // beam type
840         {
841                 self.beam_type = ReadByte();
842                 switch(self.beam_type)
843                 {
844                         case ARC_BT_MISS:
845                         {
846                                 self.beam_color = '-1 -1 1';
847                                 self.beam_alpha = 0.5;
848                                 self.beam_thickness = 8;
849                                 self.beam_traileffect = FALSE;
850                                 self.beam_hiteffect = particleeffectnum("electro_lightning");
851                                 self.beam_hitlight[0] = 0;
852                                 self.beam_hitlight[1] = 1;
853                                 self.beam_hitlight[2] = 1;
854                                 self.beam_hitlight[3] = 1;
855                                 self.beam_muzzleeffect = FALSE; //particleeffectnum("nex_muzzleflash");
856                                 self.beam_muzzlelight[0] = 0;
857                                 self.beam_muzzlelight[1] = 1;
858                                 self.beam_muzzlelight[2] = 1;
859                                 self.beam_muzzlelight[3] = 1;
860                                 self.beam_image = "particles/lgbeam";
861                                 setmodel(flash, "models/flash.md3");
862                                 flash.alpha = self.beam_alpha;
863                                 flash.colormod = self.beam_color;
864                                 flash.scale = 0.5;
865                                 break;
866                         }
867                         case ARC_BT_WALL: // grenadelauncher_muzzleflash healray_muzzleflash
868                         {
869                                 self.beam_color = '0.5 0.5 1';
870                                 self.beam_alpha = 0.5;
871                                 self.beam_thickness = 8;
872                                 self.beam_traileffect = FALSE;
873                                 self.beam_hiteffect = particleeffectnum("electro_lightning");
874                                 self.beam_hitlight[0] = 0;
875                                 self.beam_hitlight[1] = 1;
876                                 self.beam_hitlight[2] = 1;
877                                 self.beam_hitlight[3] = 1;
878                                 self.beam_muzzleeffect = FALSE; // particleeffectnum("grenadelauncher_muzzleflash");
879                                 self.beam_muzzlelight[0] = 0;
880                                 self.beam_muzzlelight[1] = 1;
881                                 self.beam_muzzlelight[2] = 1;
882                                 self.beam_muzzlelight[3] = 1;
883                                 self.beam_image = "particles/lgbeam";
884                                 setmodel(flash, "models/flash.md3");
885                                 flash.alpha = self.beam_alpha;
886                                 flash.colormod = self.beam_color;
887                                 flash.scale = 0.5;
888                                 break;
889                         }
890                         case ARC_BT_HEAL:
891                         {
892                                 self.beam_color = '0 1 0';
893                                 self.beam_alpha = 0.5;
894                                 self.beam_thickness = 8;
895                                 self.beam_traileffect = FALSE;
896                                 self.beam_hiteffect = particleeffectnum("healray_impact"); 
897                                 self.beam_hitlight[0] = 0;
898                                 self.beam_hitlight[1] = 1;
899                                 self.beam_hitlight[2] = 1;
900                                 self.beam_hitlight[3] = 1;
901                                 self.beam_muzzleeffect = FALSE; //particleeffectnum("nex_muzzleflash");
902                                 self.beam_muzzlelight[0] = 0;
903                                 self.beam_muzzlelight[1] = 1;
904                                 self.beam_muzzlelight[2] = 1;
905                                 self.beam_muzzlelight[3] = 1;
906                                 self.beam_image = "particles/lgbeam";
907                                 setmodel(flash, "models/flash.md3");
908                                 flash.alpha = self.beam_alpha;
909                                 flash.colormod = self.beam_color;
910                                 flash.scale = 0.5;
911                                 break;
912                         }
913                         case ARC_BT_HIT:
914                         {
915                                 self.beam_color = '1 0 1';
916                                 self.beam_alpha = 0.5;
917                                 self.beam_thickness = 8;
918                                 self.beam_traileffect = particleeffectnum("nex_beam");
919                                 self.beam_hiteffect = particleeffectnum("electro_lightning"); 
920                                 self.beam_hitlight[0] = 20;
921                                 self.beam_hitlight[1] = 1;
922                                 self.beam_hitlight[2] = 0;
923                                 self.beam_hitlight[3] = 0;
924                                 self.beam_muzzleeffect = FALSE; //particleeffectnum("nex_muzzleflash");
925                                 self.beam_muzzlelight[0] = 50;
926                                 self.beam_muzzlelight[1] = 1;
927                                 self.beam_muzzlelight[2] = 0;
928                                 self.beam_muzzlelight[3] = 0;
929                                 self.beam_image = "particles/lgbeam";
930                                 setmodel(flash, "models/flash.md3");
931                                 flash.alpha = self.beam_alpha;
932                                 flash.colormod = self.beam_color;
933                                 flash.scale = 0.5;
934                                 break;
935                         }
936                         case ARC_BT_BURST_MISS:
937                         {
938                                 self.beam_color = '-1 -1 1';
939                                 self.beam_alpha = 0.5;
940                                 self.beam_thickness = 14;
941                                 self.beam_traileffect = FALSE;
942                                 self.beam_hiteffect = particleeffectnum("electro_lightning"); 
943                                 self.beam_hitlight[0] = 0;
944                                 self.beam_hitlight[1] = 1;
945                                 self.beam_hitlight[2] = 1;
946                                 self.beam_hitlight[3] = 1;
947                                 self.beam_muzzleeffect = FALSE; //particleeffectnum("nex_muzzleflash");
948                                 self.beam_muzzlelight[0] = 0;
949                                 self.beam_muzzlelight[1] = 1;
950                                 self.beam_muzzlelight[2] = 1;
951                                 self.beam_muzzlelight[3] = 1;
952                                 self.beam_image = "particles/lgbeam";
953                                 setmodel(flash, "models/flash.md3");
954                                 flash.alpha = self.beam_alpha;
955                                 flash.colormod = self.beam_color;
956                                 flash.scale = 0.5;
957                                 break;
958                         }
959                         case ARC_BT_BURST_WALL:
960                         {
961                                 self.beam_color = '0.5 0.5 1';
962                                 self.beam_alpha = 0.5;
963                                 self.beam_thickness = 14;
964                                 self.beam_traileffect = FALSE;
965                                 self.beam_hiteffect = particleeffectnum("electro_lightning"); 
966                                 self.beam_hitlight[0] = 0;
967                                 self.beam_hitlight[1] = 1;
968                                 self.beam_hitlight[2] = 1;
969                                 self.beam_hitlight[3] = 1;
970                                 self.beam_muzzleeffect = FALSE; //particleeffectnum("nex_muzzleflash");
971                                 self.beam_muzzlelight[0] = 0;
972                                 self.beam_muzzlelight[1] = 1;
973                                 self.beam_muzzlelight[2] = 1;
974                                 self.beam_muzzlelight[3] = 1;
975                                 self.beam_image = "particles/lgbeam";
976                                 setmodel(flash, "models/flash.md3");
977                                 flash.alpha = self.beam_alpha;
978                                 flash.colormod = self.beam_color;
979                                 flash.scale = 0.5;
980                                 break;
981                         }
982                         case ARC_BT_BURST_HEAL:
983                         {
984                                 self.beam_color = '0 1 0';
985                                 self.beam_alpha = 0.5;
986                                 self.beam_thickness = 14;
987                                 self.beam_traileffect = FALSE;
988                                 self.beam_hiteffect = particleeffectnum("electro_lightning"); 
989                                 self.beam_hitlight[0] = 0;
990                                 self.beam_hitlight[1] = 1;
991                                 self.beam_hitlight[2] = 1;
992                                 self.beam_hitlight[3] = 1;
993                                 self.beam_muzzleeffect = FALSE; //particleeffectnum("nex_muzzleflash");
994                                 self.beam_muzzlelight[0] = 0;
995                                 self.beam_muzzlelight[1] = 1;
996                                 self.beam_muzzlelight[2] = 1;
997                                 self.beam_muzzlelight[3] = 1;
998                                 self.beam_image = "particles/lgbeam";
999                                 setmodel(flash, "models/flash.md3");
1000                                 flash.alpha = self.beam_alpha;
1001                                 flash.colormod = self.beam_color;
1002                                 flash.scale = 0.5;
1003                                 break;
1004                         }
1005                         case ARC_BT_BURST_HIT:
1006                         {
1007                                 self.beam_color = '1 0 1';
1008                                 self.beam_alpha = 0.5;
1009                                 self.beam_thickness = 14;
1010                                 self.beam_traileffect = FALSE;
1011                                 self.beam_hiteffect = particleeffectnum("electro_lightning"); 
1012                                 self.beam_hitlight[0] = 0;
1013                                 self.beam_hitlight[1] = 1;
1014                                 self.beam_hitlight[2] = 1;
1015                                 self.beam_hitlight[3] = 1;
1016                                 self.beam_muzzleeffect = FALSE; //particleeffectnum("nex_muzzleflash");
1017                                 self.beam_muzzlelight[0] = 0;
1018                                 self.beam_muzzlelight[1] = 1;
1019                                 self.beam_muzzlelight[2] = 1;
1020                                 self.beam_muzzlelight[3] = 1;
1021                                 self.beam_image = "particles/lgbeam";
1022                                 setmodel(flash, "models/flash.md3");
1023                                 flash.alpha = self.beam_alpha;
1024                                 flash.colormod = self.beam_color;
1025                                 flash.scale = 0.5;
1026                                 break;
1027                         }
1028
1029                         // shouldn't be possible, but lets make it colorful if it does :D
1030                         default:
1031                         {
1032                                 self.beam_color = randomvec();
1033                                 self.beam_alpha = 1;
1034                                 self.beam_thickness = 8;
1035                                 self.beam_traileffect = FALSE;
1036                                 self.beam_hiteffect = FALSE; 
1037                                 self.beam_hitlight[0] = 0;
1038                                 self.beam_hitlight[1] = 1;
1039                                 self.beam_hitlight[2] = 1;
1040                                 self.beam_hitlight[3] = 1;
1041                                 self.beam_muzzleeffect = FALSE; //particleeffectnum("nex_muzzleflash");
1042                                 self.beam_muzzlelight[0] = 0;
1043                                 self.beam_muzzlelight[1] = 1;
1044                                 self.beam_muzzlelight[2] = 1;
1045                                 self.beam_muzzlelight[3] = 1;
1046                                 self.beam_image = "particles/lgbeam";
1047                                 setmodel(flash, "models/flash.md3");
1048                                 flash.alpha = self.beam_alpha;
1049                                 flash.colormod = self.beam_color;
1050                                 flash.scale = 0.5;
1051                                 break;
1052                         }
1053                 }
1054         }
1055
1056         if(!self.beam_usevieworigin)
1057         {
1058                 InterpolateOrigin_Note();
1059         }
1060
1061         #if 0
1062         printf(
1063                 "Ent_ReadArcBeam(%d): sf = %d, start = %s, want = %s, dir = %s, type = %d\n",
1064                 isnew,
1065                 sf,
1066                 vtos(self.beam_start),
1067                 vtos(self.v_angle),
1068                 vtos(self.angles),
1069                 self.beam_type
1070         );
1071         #endif
1072 }
1073