]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/particles.qc
AAAAAAAAAAAGHGHGHGHGHAHGHAHGHGHHHGHH
[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_muzzleflash;
371 .string beam_image;
372
373 // WEAPONTODO: Add beam_hitlight and beam_muzzlelight which uses direct real time light control
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         InterpolateOrigin_Do();
473
474         // origin = beam starting origin
475         // v_angle = wanted/aim direction
476         // angles = current direction of beam
477
478         vector start_pos;
479         vector wantdir; //= view_forward;
480         vector beamdir; //= self.beam_dir;
481
482         float segments;
483         if(self.beam_usevieworigin)
484         {
485                 // WEAPONTODO:
486                 // Currently we have to replicate nearly the same method of figuring
487                 // out the shotdir that the server does... Ideally in the future we
488                 // should be able to acquire this from a generalized function built
489                 // into a weapon system for client code. 
490
491                 // find where we are aiming
492                 makevectors(view_angles);
493
494                 // decide upon start position
495                 if(self.beam_usevieworigin == 2)
496                         { start_pos = view_origin; }
497                 else
498                         { start_pos = self.origin; }
499
500                 // trace forward with an estimation
501                 WarpZone_TraceLine(start_pos, start_pos + view_forward * self.beam_range, MOVE_NOMONSTERS, self);
502
503                 // untransform in case our trace went through a warpzone
504                 vector vf, vr, vu;
505                 vf = view_forward;
506                 vr = view_right;
507                 vu = view_up;
508                 vector shothitpos = WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos); // warpzone support
509                 view_forward = vf;
510                 view_right = vr;
511                 view_up = vu;
512
513                 // un-adjust trueaim if shotend is too close
514                 if(vlen(shothitpos - view_origin) < g_trueaim_minrange)
515                         shothitpos = view_origin + (view_forward * g_trueaim_minrange);
516
517                 // move shot origin to the actual gun muzzle origin
518                 vector origin_offset = view_forward * self.beam_shotorigin_x + view_right * -self.beam_shotorigin_y + view_up * self.beam_shotorigin_z;
519                 start_pos = start_pos + origin_offset;
520
521                 // calculate the aim direction now
522                 wantdir = normalize(shothitpos - start_pos);
523
524                 if(!self.beam_initialized)
525                 {
526                         self.beam_dir = wantdir;
527                         self.beam_initialized = TRUE;
528                 }
529
530                 // WEAPONTODO: Calculate segments dyanmically similarly to the server code
531                 segments = 20;
532                 if(self.beam_dir != wantdir)
533                 {
534                         float angle = ceil(vlen(wantdir - self.beam_dir) * RAD2DEG);
535                         float anglelimit;
536                         if(angle && (angle > self.beam_maxangle))
537                         {
538                                 // if the angle is greater than maxangle, force the blendfactor to make this the maximum factor
539                                 anglelimit = min(self.beam_maxangle / angle, 1);
540                         }
541                         else
542                         {
543                                 // the radius is not too far yet, no worries :D
544                                 anglelimit = 1;
545                         }
546
547                         // calculate how much we're going to move the end of the beam to the want position
548                         float blendfactor = bound(0, anglelimit * (1 - (self.beam_returnspeed * frametime)), 1);
549                         self.beam_dir = normalize((wantdir * (1 - blendfactor)) + (self.beam_dir * blendfactor));
550
551                         // WEAPONTODO (server and client):
552                         // blendfactor never actually becomes 0 in this situation, which is a problem
553                         // regarding precision... this means that self.beam_dir and w_shotdir approach
554                         // eachother, however they never actually become the same value with this method.
555
556                         // Perhaps we should do some form of rounding/snapping?
557
558                         // printf("blendfactor = %f\n", blendfactor);
559
560                         #if 0
561                         // calculate how many segments are needed
562                         float max_allowed_segments;
563
564                         if(WEP_CVAR(arc, beam_distancepersegment))
565                                 max_allowed_segments = min(ARC_MAX_SEGMENTS, 1 + (vlen(w_shotdir / WEP_CVAR(arc, beam_distancepersegment))));
566                         else
567                                 max_allowed_segments = ARC_MAX_SEGMENTS;
568
569                         if(WEP_CVAR(arc, beam_degreespersegment))
570                         {
571                                 segments = min( max(1, ( min(angle, WEP_CVAR(arc, beam_maxangle)) / WEP_CVAR(arc, beam_degreespersegment) ) ), max_allowed_segments );
572                         }
573                         else
574                         {
575                                 segments = 1;
576                         }
577                         #endif
578                 }
579                 #if 0
580                 else
581                 {
582                         segments = 1;
583                 }
584                 #endif
585
586                 // set the beam direction which the rest of the code will refer to
587                 beamdir = self.beam_dir;
588
589                 // finally, set self.angles to the proper direction so that muzzle attachment points in proper direction
590                 self.angles = fixedvectoangles2(view_forward, view_up);
591         }
592         else
593         {
594                 // set the values from the provided info from the networked entity
595                 start_pos = self.origin;
596                 wantdir = self.v_angle;
597                 beamdir = self.angles;
598
599                 // WEAPONTODO: Calculate segments dyanmically similarly to the server code
600                 segments = 20;
601                 #if 0
602                 if(beamdir != wantdir)
603                 {
604                         // calculate how many segments are needed
605                         float max_allowed_segments;
606
607                         if(WEP_CVAR(arc, beam_distancepersegment))
608                                 max_allowed_segments = min(ARC_MAX_SEGMENTS, 1 + (vlen(w_shotdir / WEP_CVAR(arc, beam_distancepersegment))));
609                         else
610                                 max_allowed_segments = ARC_MAX_SEGMENTS;
611
612                         if(WEP_CVAR(arc, beam_degreespersegment))
613                         {
614                                 segments = min( max(1, ( min(angle, WEP_CVAR(arc, beam_maxangle)) / WEP_CVAR(arc, beam_degreespersegment) ) ), max_allowed_segments );
615                         }
616                         else
617                         {
618                                 segments = 1;
619                         }
620                 }
621                 else
622                 {
623                         segments = 1;
624                 }
625                 #endif
626         }
627
628         setorigin(self, start_pos);
629         self.beam_muzzleentity.angles_z = random() * 360; // WEAPONTODO: use avelocity instead?
630
631         vector beam_endpos_estimate = (start_pos + (beamdir * self.beam_range));
632
633         Draw_ArcBeam_callback_entity = self;
634         Draw_ArcBeam_callback_last_thickness = 0;
635         Draw_ArcBeam_callback_last_top = start_pos;
636         Draw_ArcBeam_callback_last_bottom = start_pos;
637
638         vector last_origin = start_pos;
639         //vector hitorigin = start_pos;
640
641         float i;
642         for(i = 1; i <= segments; ++i)
643         {
644                 // WEAPONTODO (server and client):
645                 // Segment blend and distance should probably really be calculated in a better way,
646                 // however I am not sure how to do it properly. There are a few things I have tried,
647                 // but most of them do not work properly due to my lack of understanding regarding
648                 // the mathematics behind them.
649
650                 // Ideally, we should calculate the positions along a perfect curve
651                 // between wantdir and self.beam_dir with an option for depth of arc
652
653                 // Another issue is that (on the client code) we must separate the
654                 // curve into multiple rendered curves when handling warpzones.
655                 
656                 // I can handle this by detecting it for each segment, however that
657                 // is a fairly inefficient method in comparison to having a curved line
658                 // drawing function similar to Draw_CylindricLine that accepts
659                 // top and bottom origins as input, this way there would be no
660                 // overlapping edges when connecting the curved pieces.
661
662                 // WEAPONTODO (client):
663                 // In order to do nice fading and pointing on the starting segment, we must always
664                 // have that drawn as a separate triangle... However, that is difficult to do when
665                 // keeping in mind the above problems and also optimizing the amount of segments
666                 // drawn on screen at any given time. (Automatic beam quality scaling, essentially)
667
668                 // calculate this on every segment to ensure that we always reach the full length of the attack
669                 float segmentblend = bound(0, (i/segments) + self.beam_tightness, 1);
670                 float segmentdist = vlen(beam_endpos_estimate - last_origin) * (i/segments);
671
672                 vector new_dir = normalize( (wantdir * (1 - segmentblend)) + (normalize(beam_endpos_estimate - last_origin) * segmentblend) );
673                 vector new_origin = last_origin + (new_dir * segmentdist);
674
675                 Draw_ArcBeam_callback_segmentdist = segmentdist;
676                 Draw_ArcBeam_callback_new_dir = new_dir;
677
678                 WarpZone_TraceBox_ThroughZone(
679                         last_origin,
680                         '0 0 0',
681                         '0 0 0',
682                         new_origin,
683                         MOVE_NORMAL,
684                         world,
685                         world,
686                         Draw_ArcBeam_callback
687                 );
688
689                 // check if we're going to proceed with drawing
690                 //if(trace_fraction != 1)
691                 //{
692                         // we're done with drawing this frame
693                         //last_origin = last_origin + (new_dir * segmentdist * trace_fraction);
694                         //hitorigin = last_origin + (new_dir * segmentdist * trace_fraction);
695                         //break;
696                 //}
697                 //else
698                 {
699                         // continue onto the next segment
700                         last_origin = WarpZone_TransformOrigin(WarpZone_trace_transform, new_origin);
701                         beam_endpos_estimate = WarpZone_TransformOrigin(WarpZone_trace_transform, beam_endpos_estimate);
702                 }
703         }
704
705         if(self.beam_hiteffect)
706         {
707                 pointparticles(self.beam_hiteffect, last_origin, beamdir * -1, frametime * 2);
708         }
709         if(self.beam_muzzleflash)
710         {
711                 pointparticles(self.beam_muzzleflash, start_pos + wantdir * 20, wantdir * 1000, frametime * 0.1);
712         }
713
714         // cleanup
715         Draw_ArcBeam_callback_entity = world;
716         Draw_ArcBeam_callback_new_dir = '0 0 0';
717         Draw_ArcBeam_callback_segmentdist = 0;
718         Draw_ArcBeam_callback_last_thickness = 0;
719         Draw_ArcBeam_callback_last_top = '0 0 0';
720         Draw_ArcBeam_callback_last_bottom = '0 0 0';
721 }
722
723 void Remove_ArcBeam(void)
724 {
725         remove(self.beam_muzzleentity);
726         sound(self, CH_SHOTS_SINGLE, "misc/null.wav", VOL_BASE, ATTEN_NORM);
727 }
728
729 void Ent_ReadArcBeam(float isnew)
730 {
731         float sf = ReadByte();
732         entity flash;
733
734         // self.iflags = IFLAG_ORIGIN | IFLAG_ANGLES | IFLAG_V_ANGLE; // why doesn't this work?
735         self.iflags = IFLAG_ORIGIN;
736
737         InterpolateOrigin_Undo();
738
739         if(isnew)
740         {
741                 // calculate shot origin offset from gun alignment
742                 float gunalign = autocvar_cl_gunalign;
743                 if(gunalign != 1 && gunalign != 2 && gunalign != 4)
744                         gunalign = 3; // default value
745                 --gunalign;
746
747                 self.beam_shotorigin = arc_shotorigin[gunalign];
748
749                 // set other main attributes of the beam
750                 self.draw = Draw_ArcBeam;
751                 self.entremove = Remove_ArcBeam;
752                 sound(self, CH_SHOTS_SINGLE, "weapons/lgbeam_fly.wav", VOL_BASE, ATTEN_NORM);
753
754                 flash = spawn();
755                 flash.owner = self;
756                 flash.effects = EF_ADDITIVE | EF_FULLBRIGHT;
757                 flash.drawmask = MASK_NORMAL;
758                 flash.solid = SOLID_NOT;
759                 setattachment(flash, self, "");
760                 setorigin(flash, '0 0 0');
761
762                 self.beam_muzzleentity = flash;
763         }
764         else
765         {
766                 flash = self.beam_muzzleentity;
767         }
768
769         if(sf & 1) // settings information
770         {
771                 self.beam_maxangle = ReadShort();
772                 self.beam_range = ReadCoord();
773                 self.beam_returnspeed = ReadShort();
774                 self.beam_tightness = (ReadByte() / 10);
775
776                 if(ReadByte())
777                 {
778                         if(autocvar_chase_active)
779                                 { self.beam_usevieworigin = 1; }
780                         else // use view origin
781                                 { self.beam_usevieworigin = 2; }
782                 }
783                 else
784                 {
785                         self.beam_usevieworigin = 0;
786                 }
787         }
788
789         if(sf & 2) // starting location
790         {
791                 self.origin_x = ReadCoord();
792                 self.origin_y = ReadCoord();
793                 self.origin_z = ReadCoord();
794         }
795         else if(self.beam_usevieworigin) // infer the location from player location
796         {
797                 if(self.beam_usevieworigin == 2)
798                 {
799                         // use view origin
800                         self.origin = view_origin;
801                 }
802                 else
803                 {
804                         // use player origin so that third person display still works
805                         self.origin = getplayerorigin(player_localnum) + ('0 0 1' * getstati(STAT_VIEWHEIGHT));
806                 }
807         }
808
809         setorigin(self, self.origin);
810
811         if(sf & 4) // want/aim direction
812         {
813                 self.v_angle_x = ReadCoord();
814                 self.v_angle_y = ReadCoord();
815                 self.v_angle_z = ReadCoord();
816         }
817
818         if(sf & 8) // beam direction
819         {
820                 self.angles_x = ReadCoord();
821                 self.angles_y = ReadCoord();
822                 self.angles_z = ReadCoord();
823         }
824
825         if(sf & 16) // beam type
826         {
827                 self.beam_type = ReadByte();
828                 switch(self.beam_type)
829                 {
830                         case ARC_BT_MISS:
831                         {
832                                 self.beam_color = '-1 -1 1';
833                                 self.beam_alpha = 0.5;
834                                 self.beam_thickness = 8;
835                                 self.beam_traileffect = FALSE;
836                                 self.beam_hiteffect = particleeffectnum("electro_lightning");
837                                 self.beam_muzzleflash = FALSE; //particleeffectnum("nex_muzzleflash");
838                                 self.beam_image = "particles/lgbeam";
839                                 setmodel(flash, "models/flash.md3");
840                                 flash.alpha = self.beam_alpha;
841                                 flash.colormod = self.beam_color;
842                                 flash.scale = 0.5;
843                                 break;
844                         }
845                         case ARC_BT_WALL: // grenadelauncher_muzzleflash healray_muzzleflash
846                         {
847                                 self.beam_color = '0.5 0.5 1';
848                                 self.beam_alpha = 0.5;
849                                 self.beam_thickness = 8;
850                                 self.beam_traileffect = FALSE;
851                                 self.beam_hiteffect = particleeffectnum("electro_lightning"); 
852                                 self.beam_muzzleflash = FALSE; // particleeffectnum("grenadelauncher_muzzleflash");
853                                 self.beam_image = "particles/lgbeam";
854                                 setmodel(flash, "models/flash.md3");
855                                 flash.alpha = self.beam_alpha;
856                                 flash.colormod = self.beam_color;
857                                 flash.scale = 0.5;
858                                 break;
859                         }
860                         case ARC_BT_HEAL:
861                         {
862                                 self.beam_color = '0 1 0';
863                                 self.beam_alpha = 0.5;
864                                 self.beam_thickness = 8;
865                                 self.beam_traileffect = FALSE;
866                                 self.beam_hiteffect = particleeffectnum("healray_impact"); 
867                                 self.beam_muzzleflash = FALSE; //particleeffectnum("nex_muzzleflash");
868                                 self.beam_image = "particles/lgbeam";
869                                 setmodel(flash, "models/flash.md3");
870                                 flash.alpha = self.beam_alpha;
871                                 flash.colormod = self.beam_color;
872                                 flash.scale = 0.5;
873                                 break;
874                         }
875                         case ARC_BT_HIT:
876                         {
877                                 self.beam_color = '1 0 1';
878                                 self.beam_alpha = 0.5;
879                                 self.beam_thickness = 8;
880                                 self.beam_traileffect = particleeffectnum("nex_beam");
881                                 self.beam_hiteffect = particleeffectnum("electro_lightning"); 
882                                 self.beam_muzzleflash = FALSE; //particleeffectnum("nex_muzzleflash");
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_BURST_MISS:
891                         {
892                                 self.beam_color = '-1 -1 1';
893                                 self.beam_alpha = 0.5;
894                                 self.beam_thickness = 14;
895                                 self.beam_traileffect = FALSE;
896                                 self.beam_hiteffect = particleeffectnum("electro_lightning"); 
897                                 self.beam_muzzleflash = FALSE; //particleeffectnum("nex_muzzleflash");
898                                 self.beam_image = "particles/lgbeam";
899                                 setmodel(flash, "models/flash.md3");
900                                 flash.alpha = self.beam_alpha;
901                                 flash.colormod = self.beam_color;
902                                 flash.scale = 0.5;
903                                 break;
904                         }
905                         case ARC_BT_BURST_WALL:
906                         {
907                                 self.beam_color = '0.5 0.5 1';
908                                 self.beam_alpha = 0.5;
909                                 self.beam_thickness = 14;
910                                 self.beam_traileffect = FALSE;
911                                 self.beam_hiteffect = particleeffectnum("electro_lightning"); 
912                                 self.beam_muzzleflash = FALSE; //particleeffectnum("nex_muzzleflash");
913                                 self.beam_image = "particles/lgbeam";
914                                 setmodel(flash, "models/flash.md3");
915                                 flash.alpha = self.beam_alpha;
916                                 flash.colormod = self.beam_color;
917                                 flash.scale = 0.5;
918                                 break;
919                         }
920                         case ARC_BT_BURST_HEAL:
921                         {
922                                 self.beam_color = '0 1 0';
923                                 self.beam_alpha = 0.5;
924                                 self.beam_thickness = 14;
925                                 self.beam_traileffect = FALSE;
926                                 self.beam_hiteffect = particleeffectnum("electro_lightning"); 
927                                 self.beam_muzzleflash = FALSE; //particleeffectnum("nex_muzzleflash");
928                                 self.beam_image = "particles/lgbeam";
929                                 setmodel(flash, "models/flash.md3");
930                                 flash.alpha = self.beam_alpha;
931                                 flash.colormod = self.beam_color;
932                                 flash.scale = 0.5;
933                                 break;
934                         }
935                         case ARC_BT_BURST_HIT:
936                         {
937                                 self.beam_color = '1 0 1';
938                                 self.beam_alpha = 0.5;
939                                 self.beam_thickness = 14;
940                                 self.beam_traileffect = FALSE;
941                                 self.beam_hiteffect = particleeffectnum("electro_lightning"); 
942                                 self.beam_muzzleflash = FALSE; //particleeffectnum("nex_muzzleflash");
943                                 self.beam_image = "particles/lgbeam";
944                                 setmodel(flash, "models/flash.md3");
945                                 flash.alpha = self.beam_alpha;
946                                 flash.colormod = self.beam_color;
947                                 flash.scale = 0.5;
948                                 break;
949                         }
950
951                         // shouldn't be possible, but lets make it colorful if it does :D
952                         default:
953                         {
954                                 self.beam_color = randomvec();
955                                 self.beam_alpha = 1;
956                                 self.beam_thickness = 8;
957                                 self.beam_traileffect = FALSE;
958                                 self.beam_hiteffect = FALSE; 
959                                 self.beam_muzzleflash = FALSE; //particleeffectnum("nex_muzzleflash");
960                                 self.beam_image = "particles/lgbeam";
961                                 setmodel(flash, "models/flash.md3");
962                                 flash.alpha = self.beam_alpha;
963                                 flash.colormod = self.beam_color;
964                                 flash.scale = 0.5;
965                                 break;
966                         }
967                 }
968         }
969
970         InterpolateOrigin_Note();
971
972         #if 0
973         printf(
974                 "Ent_ReadArcBeam(%d): sf = %d, start = %s, want = %s, dir = %s, type = %d\n",
975                 isnew,
976                 sf,
977                 vtos(self.beam_start),
978                 vtos(self.v_angle),
979                 vtos(self.angles),
980                 self.beam_type
981         );
982         #endif
983 }
984