Cleanup
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / weapon / arc.qc
1 #ifndef IMPLEMENTATION
2 CLASS(Arc, Weapon)
3 /* ammotype  */ ATTRIB(Arc, ammo_field, .int, ammo_cells)
4 /* impulse   */ ATTRIB(Arc, impulse, int, 3)
5 /* flags     */ ATTRIB(Arc, spawnflags, int, WEP_FLAG_NORMAL);
6 /* rating    */ ATTRIB(Arc, bot_pickupbasevalue, float, BOT_PICKUP_RATING_HIGH);
7 /* color     */ ATTRIB(Arc, wpcolor, vector, '1 1 1');
8 /* modelname */ ATTRIB(Arc, mdl, string, "arc");
9 #ifndef MENUQC
10 /* model     */ ATTRIB(Arc, m_model, Model, MDL_ARC_ITEM);
11 #endif
12 /* crosshair */ ATTRIB(Arc, w_crosshair, string, "gfx/crosshairhlac");
13 /* crosshair */ ATTRIB(Arc, w_crosshair_size, float, 0.7);
14 /* wepimg    */ ATTRIB(Arc, model2, string, "weaponarc");
15 /* refname   */ ATTRIB(Arc, netname, string, "arc");
16 /* wepname   */ ATTRIB(Arc, m_name, string, _("Arc"));
17 ENDCLASS(Arc)
18 REGISTER_WEAPON(ARC, NEW(Arc));
19
20 #define ARC_SETTINGS(w_cvar,w_prop) ARC_SETTINGS_LIST(w_cvar, w_prop, ARC, arc)
21 #define ARC_SETTINGS_LIST(w_cvar,w_prop,id,sn) \
22         w_cvar(id, sn, NONE, beam_ammo) \
23         w_cvar(id, sn, NONE, beam_animtime) \
24         w_cvar(id, sn, NONE, beam_botaimspeed) \
25         w_cvar(id, sn, NONE, beam_botaimlifetime) \
26         w_cvar(id, sn, NONE, beam_damage) \
27         w_cvar(id, sn, NONE, beam_degreespersegment) \
28         w_cvar(id, sn, NONE, beam_distancepersegment) \
29         w_cvar(id, sn, NONE, beam_falloff_halflifedist) \
30         w_cvar(id, sn, NONE, beam_falloff_maxdist) \
31         w_cvar(id, sn, NONE, beam_falloff_mindist) \
32         w_cvar(id, sn, NONE, beam_force) \
33         w_cvar(id, sn, NONE, beam_healing_amax) \
34         w_cvar(id, sn, NONE, beam_healing_aps) \
35         w_cvar(id, sn, NONE, beam_healing_hmax) \
36         w_cvar(id, sn, NONE, beam_healing_hps) \
37         w_cvar(id, sn, NONE, beam_maxangle) \
38         w_cvar(id, sn, NONE, beam_nonplayerdamage) \
39         w_cvar(id, sn, NONE, beam_range) \
40         w_cvar(id, sn, NONE, beam_refire) \
41         w_cvar(id, sn, NONE, beam_returnspeed) \
42         w_cvar(id, sn, NONE, beam_tightness) \
43         w_cvar(id, sn, NONE, burst_ammo) \
44         w_cvar(id, sn, NONE, burst_damage) \
45         w_cvar(id, sn, NONE, burst_healing_aps) \
46         w_cvar(id, sn, NONE, burst_healing_hps) \
47         w_cvar(id, sn, NONE, overheat_max)/* maximum heat before jamming */ \
48         w_cvar(id, sn, NONE, overheat_min)/* minimum heat to wait for cooldown */ \
49         w_cvar(id, sn, NONE, beam_heat)   /* heat increase per second (primary) */ \
50         w_cvar(id, sn, NONE, burst_heat)  /* heat increase per second (secondary) */ \
51         w_cvar(id, sn, NONE, cooldown)    /* heat decrease per second when resting */ \
52         w_prop(id, sn, float,  switchdelay_raise, switchdelay_raise) \
53         w_prop(id, sn, float,  switchdelay_drop, switchdelay_drop) \
54         w_prop(id, sn, string, weaponreplace, weaponreplace) \
55         w_prop(id, sn, float,  weaponstart, weaponstart) \
56         w_prop(id, sn, float,  weaponstartoverride, weaponstartoverride) \
57         w_prop(id, sn, float,  weaponthrowable, weaponthrowable)
58
59 #ifndef MENUQC
60 const float ARC_MAX_SEGMENTS = 20;
61 vector arc_shotorigin[4];
62 .vector beam_start;
63 .vector beam_dir;
64 .vector beam_wantdir;
65 .int beam_type;
66
67 const int ARC_BT_MISS =        0x00;
68 const int ARC_BT_WALL =        0x01;
69 const int ARC_BT_HEAL =        0x02;
70 const int ARC_BT_HIT =         0x03;
71 const int ARC_BT_BURST_MISS =  0x10;
72 const int ARC_BT_BURST_WALL =  0x11;
73 const int ARC_BT_BURST_HEAL =  0x12;
74 const int ARC_BT_BURST_HIT =   0x13;
75 const int ARC_BT_BURSTMASK =   0x10;
76
77 const int ARC_SF_SETTINGS =    BIT(0);
78 const int ARC_SF_START =       BIT(1);
79 const int ARC_SF_WANTDIR =     BIT(2);
80 const int ARC_SF_BEAMDIR =     BIT(3);
81 const int ARC_SF_BEAMTYPE =    BIT(4);
82 const int ARC_SF_LOCALMASK =   ARC_SF_START | ARC_SF_WANTDIR | ARC_SF_BEAMDIR;
83 #endif
84 #ifdef SVQC
85 ARC_SETTINGS(WEP_ADD_CVAR, WEP_ADD_PROP)
86 .entity arc_beam;
87 .bool arc_BUTTON_ATCK_prev; // for better animation control
88 .float beam_prev;
89 .float beam_initialized;
90 .float beam_bursting;
91 .float beam_teleporttime;
92 .float beam_heat; // (beam) amount of heat produced
93 .float arc_overheat; // (dropped arc/player) time during which it's too hot
94 .float arc_cooldown; // (dropped arc/player) cooling speed
95 .float arc_heat_percent; // (player) arc heat in [0,1] (stat)
96 .float arc_smoke_sound;
97 #endif
98 #ifdef CSQC
99
100 .vector beam_color;
101 .float beam_alpha;
102 .float beam_thickness;
103 .entity beam_traileffect;
104 .entity beam_hiteffect;
105 .float beam_hitlight[4]; // 0: radius, 123: rgb
106 .entity beam_muzzleeffect;
107 .float beam_muzzlelight[4]; // 0: radius, 123: rgb
108 .string beam_image;
109
110 .entity beam_muzzleentity;
111
112 .float beam_degreespersegment;
113 .float beam_distancepersegment;
114 .float beam_usevieworigin;
115 .float beam_initialized;
116 .float beam_maxangle;
117 .float beam_range;
118 .float beam_returnspeed;
119 .float beam_tightness;
120 .vector beam_shotorigin;
121
122 entity Draw_ArcBeam_callback_entity;
123 float Draw_ArcBeam_callback_last_thickness;
124 vector Draw_ArcBeam_callback_last_top; // NOTE: in same coordinate system as player.
125 vector Draw_ArcBeam_callback_last_bottom; // NOTE: in same coordinate system as player.
126 #endif
127 #endif
128 #ifdef IMPLEMENTATION
129 #ifdef SVQC
130 spawnfunc(weapon_arc) { weapon_defaultspawnfunc(this, WEP_ARC); }
131
132 bool W_Arc_Beam_Send(entity this, entity to, int sf)
133 {
134         WriteHeader(MSG_ENTITY, ENT_CLIENT_ARC_BEAM);
135
136         // Truncate information when this beam is displayed to the owner client
137         // - The owner client has no use for beam start position or directions,
138         //    it always figures this information out for itself with csqc code.
139         // - Spectating the owner also truncates this information.
140         float drawlocal = ((to == self.owner) || ((to.enemy == self.owner) && IS_SPEC(to)));
141         if(drawlocal) { sf &= ~ARC_SF_LOCALMASK; }
142
143         WriteByte(MSG_ENTITY, sf);
144
145         if(sf & ARC_SF_SETTINGS) // settings information
146         {
147                 WriteShort(MSG_ENTITY, WEP_CVAR(arc, beam_degreespersegment));
148                 WriteShort(MSG_ENTITY, WEP_CVAR(arc, beam_distancepersegment));
149                 WriteShort(MSG_ENTITY, WEP_CVAR(arc, beam_maxangle));
150                 WriteCoord(MSG_ENTITY, WEP_CVAR(arc, beam_range));
151                 WriteShort(MSG_ENTITY, WEP_CVAR(arc, beam_returnspeed));
152                 WriteByte(MSG_ENTITY, WEP_CVAR(arc, beam_tightness) * 10);
153
154                 WriteByte(MSG_ENTITY, drawlocal);
155         }
156         if(sf & ARC_SF_START) // starting location
157         {
158                 WriteCoord(MSG_ENTITY, self.beam_start.x);
159                 WriteCoord(MSG_ENTITY, self.beam_start.y);
160                 WriteCoord(MSG_ENTITY, self.beam_start.z);
161         }
162         if(sf & ARC_SF_WANTDIR) // want/aim direction
163         {
164                 WriteCoord(MSG_ENTITY, self.beam_wantdir.x);
165                 WriteCoord(MSG_ENTITY, self.beam_wantdir.y);
166                 WriteCoord(MSG_ENTITY, self.beam_wantdir.z);
167         }
168         if(sf & ARC_SF_BEAMDIR) // beam direction
169         {
170                 WriteCoord(MSG_ENTITY, self.beam_dir.x);
171                 WriteCoord(MSG_ENTITY, self.beam_dir.y);
172                 WriteCoord(MSG_ENTITY, self.beam_dir.z);
173         }
174         if(sf & ARC_SF_BEAMTYPE) // beam type
175         {
176                 WriteByte(MSG_ENTITY, self.beam_type);
177         }
178
179         return true;
180 }
181
182 void Reset_ArcBeam(entity player, vector forward)
183 {
184         if (!player.arc_beam) {
185                 return;
186         }
187         player.arc_beam.beam_dir = forward;
188         player.arc_beam.beam_teleporttime = time;
189 }
190
191 float Arc_GetHeat_Percent(entity player)
192 {
193         if ( WEP_CVAR(arc, overheat_max) <= 0 ||  WEP_CVAR(arc, overheat_max) <= 0 )
194         {
195                 player.arc_overheat = 0;
196                 return 0;
197         }
198
199         if ( player.arc_beam )
200                 return player.arc_beam.beam_heat/WEP_CVAR(arc, overheat_max);
201
202         if ( player.arc_overheat > time )
203         {
204                 return (player.arc_overheat-time) / WEP_CVAR(arc, overheat_max)
205                         * player.arc_cooldown;
206         }
207
208         return 0;
209 }
210 void Arc_Player_SetHeat(entity player)
211 {
212         player.arc_heat_percent = Arc_GetHeat_Percent(player);
213         //dprint("Heat: ",ftos(player.arc_heat_percent*100),"%\n");
214 }
215
216 void W_Arc_Beam_Think()
217 {SELFPARAM();
218         if(self != self.owner.arc_beam)
219         {
220                 remove(self);
221                 return;
222         }
223
224
225         float burst = 0;
226         if( self.owner.BUTTON_ATCK2 || self.beam_bursting)
227         {
228                 if(!self.beam_bursting)
229                         self.beam_bursting = true;
230                 burst = ARC_BT_BURSTMASK;
231         }
232
233         if(
234                 !IS_PLAYER(self.owner)
235                 ||
236                 (self.owner.WEP_AMMO(ARC) <= 0 && !(self.owner.items & IT_UNLIMITED_WEAPON_AMMO))
237                 ||
238                 self.owner.deadflag != DEAD_NO
239                 ||
240                 (!self.owner.BUTTON_ATCK && !burst )
241                 ||
242                 self.owner.frozen
243                 ||
244                 self.owner.vehicle
245                 ||
246                 (WEP_CVAR(arc, overheat_max) > 0 && self.beam_heat >= WEP_CVAR(arc, overheat_max))
247         )
248         {
249                 if ( WEP_CVAR(arc, cooldown) > 0 )
250                 {
251                         float cooldown_speed = 0;
252                         if ( self.beam_heat > WEP_CVAR(arc, overheat_min) && WEP_CVAR(arc, cooldown) > 0 )
253                         {
254                                 cooldown_speed = WEP_CVAR(arc, cooldown);
255                         }
256                         else if ( !burst )
257                         {
258                                 cooldown_speed = self.beam_heat / WEP_CVAR(arc, beam_refire);
259                         }
260
261                         if ( cooldown_speed )
262                         {
263                                 self.owner.arc_overheat = time + self.beam_heat / cooldown_speed;
264                                 self.owner.arc_cooldown = cooldown_speed;
265                         }
266
267                         if ( WEP_CVAR(arc, overheat_max) > 0 && self.beam_heat >= WEP_CVAR(arc, overheat_max) )
268                         {
269                                 Send_Effect(EFFECT_ARC_OVERHEAT,
270                                         self.beam_start, self.beam_wantdir, 1 );
271                                 sound(self, CH_WEAPON_A, SND_ARC_STOP, VOL_BASE, ATTN_NORM);
272                         }
273                 }
274
275                 if(self == self.owner.arc_beam) { self.owner.arc_beam = world; }
276                 setself(self.owner);
277                 Weapon w = WEP_ARC;
278                 if(!w.wr_checkammo1(w) && !w.wr_checkammo2(w))
279                 if(!(self.items & IT_UNLIMITED_WEAPON_AMMO))
280                 {
281                         // note: this doesn't force the switch
282                         W_SwitchToOtherWeapon(self);
283                 }
284                 setself(this);
285                 remove(self);
286                 return;
287         }
288
289         // decrease ammo
290         float coefficient = frametime;
291         if(!(self.owner.items & IT_UNLIMITED_WEAPON_AMMO))
292         {
293                 float rootammo;
294                 if(burst)
295                         { rootammo = WEP_CVAR(arc, burst_ammo); }
296                 else
297                         { rootammo = WEP_CVAR(arc, beam_ammo); }
298
299                 if(rootammo)
300                 {
301                         coefficient = min(coefficient, self.owner.WEP_AMMO(ARC) / rootammo);
302                         self.owner.WEP_AMMO(ARC) = max(0, self.owner.WEP_AMMO(ARC) - (rootammo * frametime));
303                 }
304         }
305         float heat_speed = burst ? WEP_CVAR(arc, burst_heat) : WEP_CVAR(arc, beam_heat);
306         self.beam_heat = min( WEP_CVAR(arc, overheat_max), self.beam_heat + heat_speed*frametime );
307
308         makevectors(self.owner.v_angle);
309
310         W_SetupShot_Range(
311                 self.owner,
312                 true,
313                 0,
314                 "",
315                 0,
316                 WEP_CVAR(arc, beam_damage) * coefficient,
317                 WEP_CVAR(arc, beam_range)
318         );
319
320         // After teleport, "lock" the beam until the teleport is confirmed.
321         if (time < self.beam_teleporttime + ANTILAG_LATENCY(self.owner)) {
322                 w_shotdir = self.beam_dir;
323         }
324
325         // network information: shot origin and want/aim direction
326         if(self.beam_start != w_shotorg)
327         {
328                 self.SendFlags |= ARC_SF_START;
329                 self.beam_start = w_shotorg;
330         }
331         if(self.beam_wantdir != w_shotdir)
332         {
333                 self.SendFlags |= ARC_SF_WANTDIR;
334                 self.beam_wantdir = w_shotdir;
335         }
336
337         if(!self.beam_initialized)
338         {
339                 self.beam_dir = w_shotdir;
340                 self.beam_initialized = true;
341         }
342
343         // WEAPONTODO: Detect player velocity so that the beam curves when moving too
344         // idea: blend together self.beam_dir with the inverted direction the player is moving in
345         // might have to make some special accomodation so that it only uses view_right and view_up
346
347         // note that if we do this, it'll always be corrected to a maximum angle by beam_maxangle handling
348
349         float segments;
350         if(self.beam_dir != w_shotdir)
351         {
352                 // calculate how much we're going to move the end of the beam to the want position
353                 // WEAPONTODO (server and client):
354                 // blendfactor never actually becomes 0 in this situation, which is a problem
355                 // regarding precision... this means that self.beam_dir and w_shotdir approach
356                 // eachother, however they never actually become the same value with this method.
357                 // Perhaps we should do some form of rounding/snapping?
358                 float angle = vlen(w_shotdir - self.beam_dir) * RAD2DEG;
359                 if(angle && (angle > WEP_CVAR(arc, beam_maxangle)))
360                 {
361                         // if the angle is greater than maxangle, force the blendfactor to make this the maximum factor
362                         float blendfactor = bound(
363                                 0,
364                                 (1 - (WEP_CVAR(arc, beam_returnspeed) * frametime)),
365                                 min(WEP_CVAR(arc, beam_maxangle) / angle, 1)
366                         );
367                         self.beam_dir = normalize((w_shotdir * (1 - blendfactor)) + (self.beam_dir * blendfactor));
368                 }
369                 else
370                 {
371                         // the radius is not too far yet, no worries :D
372                         float blendfactor = bound(
373                                 0,
374                                 (1 - (WEP_CVAR(arc, beam_returnspeed) * frametime)),
375                                 1
376                         );
377                         self.beam_dir = normalize((w_shotdir * (1 - blendfactor)) + (self.beam_dir * blendfactor));
378                 }
379
380                 // network information: beam direction
381                 self.SendFlags |= ARC_SF_BEAMDIR;
382
383                 // calculate how many segments are needed
384                 float max_allowed_segments;
385
386                 if(WEP_CVAR(arc, beam_distancepersegment))
387                 {
388                         max_allowed_segments = min(
389                                 ARC_MAX_SEGMENTS,
390                                 1 + (vlen(w_shotdir / WEP_CVAR(arc, beam_distancepersegment)))
391                         );
392                 }
393                 else { max_allowed_segments = ARC_MAX_SEGMENTS; }
394
395                 if(WEP_CVAR(arc, beam_degreespersegment))
396                 {
397                         segments = bound(
398                                 1,
399                                 (
400                                         min(
401                                                 angle,
402                                                 WEP_CVAR(arc, beam_maxangle)
403                                         )
404                                         /
405                                         WEP_CVAR(arc, beam_degreespersegment)
406                                 ),
407                                 max_allowed_segments
408                         );
409                 }
410                 else { segments = 1; }
411         }
412         else { segments = 1; }
413
414         vector beam_endpos = (w_shotorg + (self.beam_dir * WEP_CVAR(arc, beam_range)));
415         vector beam_controlpoint = w_shotorg + w_shotdir * (WEP_CVAR(arc, beam_range) * (1 - WEP_CVAR(arc, beam_tightness)));
416
417         float i;
418         float new_beam_type = 0;
419         vector last_origin = w_shotorg;
420         for(i = 1; i <= segments; ++i)
421         {
422                 // WEAPONTODO (client):
423                 // In order to do nice fading and pointing on the starting segment, we must always
424                 // have that drawn as a separate triangle... However, that is difficult to do when
425                 // keeping in mind the above problems and also optimizing the amount of segments
426                 // drawn on screen at any given time. (Automatic beam quality scaling, essentially)
427
428                 vector new_origin = bezier_quadratic_getpoint(
429                         w_shotorg,
430                         beam_controlpoint,
431                         beam_endpos,
432                         i / segments);
433                 vector new_dir = normalize(new_origin - last_origin);
434
435                 WarpZone_traceline_antilag(
436                         self.owner,
437                         last_origin,
438                         new_origin,
439                         MOVE_NORMAL,
440                         self.owner,
441                         ANTILAG_LATENCY(self.owner)
442                 );
443
444                 // Do all the transforms for warpzones right now, as we already
445                 // "are" in the post-trace system (if we hit a player, that's
446                 // always BEHIND the last passed wz).
447                 last_origin = trace_endpos;
448                 w_shotorg = WarpZone_TransformOrigin(WarpZone_trace_transform, w_shotorg);
449                 beam_controlpoint = WarpZone_TransformOrigin(WarpZone_trace_transform, beam_controlpoint);
450                 beam_endpos = WarpZone_TransformOrigin(WarpZone_trace_transform, beam_endpos);
451                 new_dir = WarpZone_TransformVelocity(WarpZone_trace_transform, new_dir);
452
453                 float is_player = (
454                         IS_PLAYER(trace_ent)
455                         ||
456                         trace_ent.classname == "body"
457                         ||
458                         IS_MONSTER(trace_ent)
459                 );
460
461                 if(trace_ent && trace_ent.takedamage && (is_player || WEP_CVAR(arc, beam_nonplayerdamage)))
462                 {
463                         // calculate our own hit origin as trace_endpos tends to jump around annoyingly (to player origin?)
464                         // NO. trace_endpos should be just fine. If not,
465                         // that's an engine bug that needs proper debugging.
466                         vector hitorigin = trace_endpos;
467
468                         float falloff = ExponentialFalloff(
469                                 WEP_CVAR(arc, beam_falloff_mindist),
470                                 WEP_CVAR(arc, beam_falloff_maxdist),
471                                 WEP_CVAR(arc, beam_falloff_halflifedist),
472                                 vlen(WarpZone_UnTransformOrigin(WarpZone_trace_transform, hitorigin) - w_shotorg)
473                         );
474
475                         if(is_player && SAME_TEAM(self.owner, trace_ent))
476                         {
477                                 float roothealth, rootarmor;
478                                 if(burst)
479                                 {
480                                         roothealth = WEP_CVAR(arc, burst_healing_hps);
481                                         rootarmor = WEP_CVAR(arc, burst_healing_aps);
482                                 }
483                                 else
484                                 {
485                                         roothealth = WEP_CVAR(arc, beam_healing_hps);
486                                         rootarmor = WEP_CVAR(arc, beam_healing_aps);
487                                 }
488
489                                 if(trace_ent.health <= WEP_CVAR(arc, beam_healing_hmax) && roothealth)
490                                 {
491                                         trace_ent.health = min(
492                                                 trace_ent.health + (roothealth * coefficient),
493                                                 WEP_CVAR(arc, beam_healing_hmax)
494                                         );
495                                 }
496                                 if(trace_ent.armorvalue <= WEP_CVAR(arc, beam_healing_amax) && rootarmor)
497                                 {
498                                         trace_ent.armorvalue = min(
499                                                 trace_ent.armorvalue + (rootarmor * coefficient),
500                                                 WEP_CVAR(arc, beam_healing_amax)
501                                         );
502                                 }
503
504                                 // stop rot, set visual effect
505                                 if(roothealth || rootarmor)
506                                 {
507                                         trace_ent.pauserothealth_finished = max(
508                                                 trace_ent.pauserothealth_finished,
509                                                 time + autocvar_g_balance_pause_health_rot
510                                         );
511                                         trace_ent.pauserotarmor_finished = max(
512                                                 trace_ent.pauserotarmor_finished,
513                                                 time + autocvar_g_balance_pause_armor_rot
514                                         );
515                                         new_beam_type = ARC_BT_HEAL;
516                                 }
517                         }
518                         else
519                         {
520                                 float rootdamage;
521                                 if(is_player)
522                                 {
523                                         if(burst)
524                                                 { rootdamage = WEP_CVAR(arc, burst_damage); }
525                                         else
526                                                 { rootdamage = WEP_CVAR(arc, beam_damage); }
527                                 }
528                                 else
529                                         { rootdamage = WEP_CVAR(arc, beam_nonplayerdamage); }
530
531                                 if(accuracy_isgooddamage(self.owner, trace_ent))
532                                 {
533                                         accuracy_add(
534                                                 self.owner,
535                                                 WEP_ARC.m_id,
536                                                 0,
537                                                 rootdamage * coefficient * falloff
538                                         );
539                                 }
540
541                                 Damage(
542                                         trace_ent,
543                                         self.owner,
544                                         self.owner,
545                                         rootdamage * coefficient * falloff,
546                                         WEP_ARC.m_id,
547                                         hitorigin,
548                                         WEP_CVAR(arc, beam_force) * new_dir * coefficient * falloff
549                                 );
550
551                                 new_beam_type = ARC_BT_HIT;
552                         }
553                         break;
554                 }
555                 else if(trace_fraction != 1)
556                 {
557                         // we collided with geometry
558                         new_beam_type = ARC_BT_WALL;
559                         break;
560                 }
561         }
562
563         // te_explosion(trace_endpos);
564
565         // if we're bursting, use burst visual effects
566         new_beam_type |= burst;
567
568         // network information: beam type
569         if(new_beam_type != self.beam_type)
570         {
571                 self.SendFlags |= ARC_SF_BEAMTYPE;
572                 self.beam_type = new_beam_type;
573         }
574
575         self.owner.beam_prev = time;
576         self.nextthink = time;
577 }
578
579 void W_Arc_Beam(float burst)
580 {SELFPARAM();
581
582         // only play fire sound if 1 sec has passed since player let go the fire button
583         if(time - self.beam_prev > 1)
584                 sound(self, CH_WEAPON_A, SND_ARC_FIRE, VOL_BASE, ATTN_NORM);
585
586         entity beam = self.arc_beam = new(W_Arc_Beam);
587         beam.solid = SOLID_NOT;
588         beam.think = W_Arc_Beam_Think;
589         beam.owner = self;
590         beam.movetype = MOVETYPE_NONE;
591         beam.bot_dodge = true;
592         beam.bot_dodgerating = WEP_CVAR(arc, beam_damage);
593         beam.beam_bursting = burst;
594         Net_LinkEntity(beam, false, 0, W_Arc_Beam_Send);
595
596         WITH(entity, self, beam, beam.think());
597 }
598
599 void Arc_Smoke()
600 {SELFPARAM();
601         makevectors(self.v_angle);
602         W_SetupShot_Range(self,true,0,"",0,0,0);
603
604         vector smoke_origin = w_shotorg + self.velocity*frametime;
605         if ( self.arc_overheat > time )
606         {
607                 if ( random() < self.arc_heat_percent )
608                         Send_Effect(EFFECT_ARC_SMOKE, smoke_origin, '0 0 0', 1 );
609                 if ( self.BUTTON_ATCK || self.BUTTON_ATCK2 )
610                 {
611                         Send_Effect(EFFECT_ARC_OVERHEAT_FIRE, smoke_origin, w_shotdir, 1 );
612                         if ( !self.arc_smoke_sound )
613                         {
614                                 self.arc_smoke_sound = 1;
615                                 sound(self, CH_SHOTS_SINGLE, SND_ARC_LOOP_OVERHEAT, VOL_BASE, ATTN_NORM);
616                         }
617                 }
618         }
619         else if ( self.arc_beam && WEP_CVAR(arc, overheat_max) > 0 &&
620                         self.arc_beam.beam_heat > WEP_CVAR(arc, overheat_min) )
621         {
622                 if ( random() < (self.arc_beam.beam_heat-WEP_CVAR(arc, overheat_min)) /
623                                 ( WEP_CVAR(arc, overheat_max)-WEP_CVAR(arc, overheat_min) ) )
624                         Send_Effect(EFFECT_ARC_SMOKE, smoke_origin, '0 0 0', 1 );
625         }
626
627         if (  self.arc_smoke_sound && ( self.arc_overheat <= time ||
628                 !( self.BUTTON_ATCK || self.BUTTON_ATCK2 ) ) || self.switchweapon != WEP_ARC.m_id )
629         {
630                 self.arc_smoke_sound = 0;
631                 sound(self, CH_SHOTS_SINGLE, SND_Null, VOL_BASE, ATTEN_NORM);
632         }
633 }
634
635                 METHOD(Arc, wr_aim, void(entity thiswep))
636                 {
637                         SELFPARAM();
638                         if(WEP_CVAR(arc, beam_botaimspeed))
639                         {
640                                 self.BUTTON_ATCK = bot_aim(
641                                         WEP_CVAR(arc, beam_botaimspeed),
642                                         0,
643                                         WEP_CVAR(arc, beam_botaimlifetime),
644                                         false
645                                 );
646                         }
647                         else
648                         {
649                                 self.BUTTON_ATCK = bot_aim(
650                                         1000000,
651                                         0,
652                                         0.001,
653                                         false
654                                 );
655                         }
656                 }
657                 METHOD(Arc, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
658                 {
659                         Arc_Player_SetHeat(actor);
660                         Arc_Smoke();
661
662                         if (time >= actor.arc_overheat)
663                         if ((fire & 1) || (fire & 2) || actor.arc_beam.beam_bursting)
664                         {
665
666                                 if(actor.arc_BUTTON_ATCK_prev)
667                                 {
668                                         #if 0
669                                         if(actor.animstate_startframe == actor.anim_shoot.x && actor.animstate_numframes == actor.anim_shoot.y)
670                                                 weapon_thinkf(actor, weaponentity, WFRAME_DONTCHANGE, autocvar_g_balance_arc_primary_animtime, w_ready);
671                                         else
672                                         #endif
673                                                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(arc, beam_animtime), w_ready);
674                                 }
675
676                                 if((!actor.arc_beam) || wasfreed(actor.arc_beam))
677                                 {
678                                         if(weapon_prepareattack(thiswep, actor, weaponentity, boolean(fire & 2), 0))
679                                         {
680                                                 W_Arc_Beam(boolean(fire & 2));
681
682                                                 if(!actor.arc_BUTTON_ATCK_prev)
683                                                 {
684                                                         weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(arc, beam_animtime), w_ready);
685                                                         actor.arc_BUTTON_ATCK_prev = true;
686                                                 }
687                                         }
688                                 }
689
690                                 return;
691                         }
692
693                         if(actor.arc_BUTTON_ATCK_prev)
694                         {
695                                 sound(actor, CH_WEAPON_A, SND_ARC_STOP, VOL_BASE, ATTN_NORM);
696                                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(arc, beam_animtime), w_ready);
697                                 int slot = weaponslot(weaponentity);
698                                 ATTACK_FINISHED(actor, slot) = time + WEP_CVAR(arc, beam_refire) * W_WeaponRateFactor();
699                         }
700                         actor.arc_BUTTON_ATCK_prev = false;
701
702                         #if 0
703                         if(fire & 2)
704                         if(weapon_prepareattack(thiswep, actor, weaponentity, true, autocvar_g_balance_arc_secondary_refire))
705                         {
706                                 W_Arc_Attack2();
707                                 actor.arc_count = autocvar_g_balance_arc_secondary_count;
708                                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, autocvar_g_balance_arc_secondary_animtime, w_arc_checkattack);
709                                 actor.arc_secondarytime = time + autocvar_g_balance_arc_secondary_refire2 * W_WeaponRateFactor();
710                         }
711                         #endif
712                 }
713                 METHOD(Arc, wr_init, void(entity thiswep))
714                 {
715                         if(!arc_shotorigin[0])
716                         {
717                                 arc_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ARC.m_id), false, false, 1);
718                                 arc_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ARC.m_id), false, false, 2);
719                                 arc_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ARC.m_id), false, false, 3);
720                                 arc_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ARC.m_id), false, false, 4);
721                         }
722                         ARC_SETTINGS(WEP_SKIP_CVAR, WEP_SET_PROP);
723                 }
724                 METHOD(Arc, wr_checkammo1, bool(entity thiswep))
725                 {
726                         SELFPARAM();
727                         return ((!WEP_CVAR(arc, beam_ammo)) || (self.WEP_AMMO(ARC) > 0));
728                 }
729                 METHOD(Arc, wr_checkammo2, bool(entity thiswep))
730                 {
731                         SELFPARAM();
732                         return WEP_CVAR(arc, overheat_max) > 0 &&
733                                 ((!WEP_CVAR(arc, burst_ammo)) || (self.WEP_AMMO(ARC) > 0));
734                 }
735                 METHOD(Arc, wr_config, void(entity thiswep))
736                 {
737                         ARC_SETTINGS(WEP_CONFIG_WRITE_CVARS, WEP_CONFIG_WRITE_PROPS);
738                 }
739                 METHOD(Arc, wr_killmessage, int(entity thiswep))
740                 {
741                         return WEAPON_ARC_MURDER;
742                 }
743                 METHOD(Arc, wr_drop, void(entity thiswep))
744                 {
745                         weapon_dropevent_item.arc_overheat = self.arc_overheat;
746                         weapon_dropevent_item.arc_cooldown = self.arc_cooldown;
747                         self.arc_overheat = 0;
748                         self.arc_cooldown = 0;
749                 }
750                 METHOD(Arc, wr_pickup, void(entity thiswep))
751                 {
752                         if ( !client_hasweapon(self, WEP_ARC.m_id, false, false) &&
753                                 weapon_dropevent_item.arc_overheat > time )
754                         {
755                                 self.arc_overheat = weapon_dropevent_item.arc_overheat;
756                                 self.arc_cooldown = weapon_dropevent_item.arc_cooldown;
757                         }
758                 }
759 #endif
760 #ifdef CSQC
761 void Draw_ArcBeam_callback(vector start, vector hit, vector end)
762 {
763         entity beam = Draw_ArcBeam_callback_entity;
764         vector transformed_view_org;
765         transformed_view_org = WarpZone_TransformOrigin(WarpZone_trace_transform, view_origin);
766
767         // Thickdir shall be perpendicular to the beam and to the view-to-beam direction (WEAPONTODO: WHY)
768         // WEAPONTODO: Wouldn't it be better to be perpendicular to the beam and to the view FORWARD direction?
769         vector thickdir = normalize(cross(normalize(start - hit), transformed_view_org - start));
770
771         vector hitorigin;
772
773         // draw segment
774         #if 0
775         if(trace_fraction != 1)
776         {
777                 // calculate our own hit origin as trace_endpos tends to jump around annoyingly (to player origin?)
778                 hitorigin = start + (Draw_ArcBeam_callback_new_dir * Draw_ArcBeam_callback_segmentdist * trace_fraction);
779                 hitorigin = WarpZone_TransformOrigin(WarpZone_trace_transform, hitorigin);
780         }
781         else
782         {
783                 hitorigin = hit;
784         }
785         #else
786         hitorigin = hit;
787         #endif
788
789         // decide upon thickness
790         float thickness = beam.beam_thickness;
791
792         // draw primary beam render
793         vector top    = hitorigin + (thickdir * thickness);
794         vector bottom = hitorigin - (thickdir * thickness);
795
796         vector last_top = WarpZone_TransformOrigin(WarpZone_trace_transform, Draw_ArcBeam_callback_last_top);
797         vector last_bottom = WarpZone_TransformOrigin(WarpZone_trace_transform, Draw_ArcBeam_callback_last_bottom);
798
799         R_BeginPolygon(beam.beam_image, DRAWFLAG_NORMAL); // DRAWFLAG_ADDITIVE
800         R_PolygonVertex(
801                 top,
802                 '0 0.5 0' + ('0 0.5 0' * (thickness / beam.beam_thickness)),
803                 beam.beam_color,
804                 beam.beam_alpha
805         );
806         R_PolygonVertex(
807                 last_top,
808                 '0 0.5 0' + ('0 0.5 0' * (Draw_ArcBeam_callback_last_thickness / beam.beam_thickness)),
809                 beam.beam_color,
810                 beam.beam_alpha
811         );
812         R_PolygonVertex(
813                 last_bottom,
814                 '0 0.5 0' * (1 - (Draw_ArcBeam_callback_last_thickness / beam.beam_thickness)),
815                 beam.beam_color,
816                 beam.beam_alpha
817         );
818         R_PolygonVertex(
819                 bottom,
820                 '0 0.5 0' * (1 - (thickness / beam.beam_thickness)),
821                 beam.beam_color,
822                 beam.beam_alpha
823         );
824         R_EndPolygon();
825
826         // draw trailing particles
827         // NOTES:
828         //  - Don't use spammy particle counts here, use a FEW small particles around the beam
829         //  - We're not using WarpZone_TrailParticles here because we will handle warpzones ourselves.
830         if(beam.beam_traileffect)
831         {
832                 trailparticles(beam, beam.beam_traileffect, start, hitorigin);
833         }
834
835         // set up for the next
836         Draw_ArcBeam_callback_last_thickness = thickness;
837         Draw_ArcBeam_callback_last_top = WarpZone_UnTransformOrigin(WarpZone_trace_transform, top);
838         Draw_ArcBeam_callback_last_bottom = WarpZone_UnTransformOrigin(WarpZone_trace_transform, bottom);
839 }
840
841 void Reset_ArcBeam()
842 {
843         entity e;
844         for (e = world; (e = findfloat(e, beam_usevieworigin, 1)); ) {
845                 e.beam_initialized = false;
846         }
847         for (e = world; (e = findfloat(e, beam_usevieworigin, 2)); ) {
848                 e.beam_initialized = false;
849         }
850 }
851
852 void Draw_ArcBeam(entity this)
853 {
854         float dt = time - self.move_time;
855         self.move_time = time;
856         if(dt <= 0) { return; }
857
858         if(!self.beam_usevieworigin)
859         {
860                 InterpolateOrigin_Do();
861         }
862
863         // origin = beam starting origin
864         // v_angle = wanted/aim direction
865         // angles = current direction of beam
866
867         vector start_pos;
868         vector wantdir; //= view_forward;
869         vector beamdir; //= self.beam_dir;
870
871         float segments;
872         if(self.beam_usevieworigin)
873         {
874                 // WEAPONTODO:
875                 // Currently we have to replicate nearly the same method of figuring
876                 // out the shotdir that the server does... Ideally in the future we
877                 // should be able to acquire this from a generalized function built
878                 // into a weapon system for client code.
879
880                 // find where we are aiming
881                 makevectors(warpzone_save_view_angles);
882                 vector forward = v_forward;
883                 vector right = v_right;
884                 vector up = v_up;
885
886                 // decide upon start position
887                 if(self.beam_usevieworigin == 2)
888                         { start_pos = warpzone_save_view_origin; }
889                 else
890                         { start_pos = self.origin; }
891
892                 // trace forward with an estimation
893                 WarpZone_TraceLine(
894                         start_pos,
895                         start_pos + forward * self.beam_range,
896                         MOVE_NOMONSTERS,
897                         self
898                 );
899
900                 // untransform in case our trace went through a warpzone
901                 vector end_pos = WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos);
902
903                 // un-adjust trueaim if shotend is too close
904                 if(vlen(end_pos - start_pos) < g_trueaim_minrange)
905                         end_pos = start_pos + (forward * g_trueaim_minrange);
906
907                 // move shot origin to the actual gun muzzle origin
908                 vector origin_offset =
909                           right * -self.beam_shotorigin.y
910                         + up * self.beam_shotorigin.z;
911
912                 start_pos = start_pos + origin_offset;
913
914                 // Move it also forward, but only as far as possible without hitting anything. Don't poke into walls!
915                 traceline(start_pos, start_pos + forward * self.beam_shotorigin.x, MOVE_NORMAL, self);
916                 start_pos = trace_endpos;
917
918                 // calculate the aim direction now
919                 wantdir = normalize(end_pos - start_pos);
920
921                 if(!self.beam_initialized)
922                 {
923                         self.beam_dir = wantdir;
924                         self.beam_initialized = true;
925                 }
926
927                 if(self.beam_dir != wantdir)
928                 {
929                         // calculate how much we're going to move the end of the beam to the want position
930                         // WEAPONTODO (server and client):
931                         // blendfactor never actually becomes 0 in this situation, which is a problem
932                         // regarding precision... this means that self.beam_dir and w_shotdir approach
933                         // eachother, however they never actually become the same value with this method.
934                         // Perhaps we should do some form of rounding/snapping?
935                         float angle = vlen(wantdir - self.beam_dir) * RAD2DEG;
936                         if(angle && (angle > self.beam_maxangle))
937                         {
938                                 // if the angle is greater than maxangle, force the blendfactor to make this the maximum factor
939                                 float blendfactor = bound(
940                                         0,
941                                         (1 - (self.beam_returnspeed * frametime)),
942                                         min(self.beam_maxangle / angle, 1)
943                                 );
944                                 self.beam_dir = normalize((wantdir * (1 - blendfactor)) + (self.beam_dir * blendfactor));
945                         }
946                         else
947                         {
948                                 // the radius is not too far yet, no worries :D
949                                 float blendfactor = bound(
950                                         0,
951                                         (1 - (self.beam_returnspeed * frametime)),
952                                         1
953                                 );
954                                 self.beam_dir = normalize((wantdir * (1 - blendfactor)) + (self.beam_dir * blendfactor));
955                         }
956
957                         // calculate how many segments are needed
958                         float max_allowed_segments;
959
960                         if(self.beam_distancepersegment)
961                         {
962                                 max_allowed_segments = min(
963                                         ARC_MAX_SEGMENTS,
964                                         1 + (vlen(wantdir / self.beam_distancepersegment))
965                                 );
966                         }
967                         else { max_allowed_segments = ARC_MAX_SEGMENTS; }
968
969                         if(self.beam_degreespersegment)
970                         {
971                                 segments = bound(
972                                         1,
973                                         (
974                                                 min(
975                                                         angle,
976                                                         self.beam_maxangle
977                                                 )
978                                                 /
979                                                 self.beam_degreespersegment
980                                         ),
981                                         max_allowed_segments
982                                 );
983                         }
984                         else { segments = 1; }
985                 }
986                 else { segments = 1; }
987
988                 // set the beam direction which the rest of the code will refer to
989                 beamdir = self.beam_dir;
990
991                 // finally, set self.angles to the proper direction so that muzzle attachment points in proper direction
992                 self.angles = fixedvectoangles2(forward, up); // TODO(Samual): is this == warpzone_save_view_angles?
993         }
994         else
995         {
996                 // set the values from the provided info from the networked entity
997                 start_pos = self.origin;
998                 wantdir = self.v_angle;
999                 beamdir = self.angles;
1000
1001                 if(beamdir != wantdir)
1002                 {
1003                         float angle = vlen(wantdir - beamdir) * RAD2DEG;
1004
1005                         // calculate how many segments are needed
1006                         float max_allowed_segments;
1007
1008                         if(self.beam_distancepersegment)
1009                         {
1010                                 max_allowed_segments = min(
1011                                         ARC_MAX_SEGMENTS,
1012                                         1 + (vlen(wantdir / self.beam_distancepersegment))
1013                                 );
1014                         }
1015                         else { max_allowed_segments = ARC_MAX_SEGMENTS; }
1016
1017                         if(self.beam_degreespersegment)
1018                         {
1019                                 segments = bound(
1020                                         1,
1021                                         (
1022                                                 min(
1023                                                         angle,
1024                                                         self.beam_maxangle
1025                                                 )
1026                                                 /
1027                                                 self.beam_degreespersegment
1028                                         ),
1029                                         max_allowed_segments
1030                                 );
1031                         }
1032                         else { segments = 1; }
1033                 }
1034                 else { segments = 1; }
1035         }
1036
1037         setorigin(self, start_pos);
1038         self.beam_muzzleentity.angles_z = random() * 360; // WEAPONTODO: use avelocity instead?
1039
1040         vector beam_endpos = (start_pos + (beamdir * self.beam_range));
1041         vector beam_controlpoint = start_pos + wantdir * (self.beam_range * (1 - self.beam_tightness));
1042
1043         Draw_ArcBeam_callback_entity = self;
1044         Draw_ArcBeam_callback_last_thickness = 0;
1045         Draw_ArcBeam_callback_last_top = start_pos;
1046         Draw_ArcBeam_callback_last_bottom = start_pos;
1047
1048         vector last_origin = start_pos;
1049         vector original_start_pos = start_pos;
1050
1051         float i;
1052         for(i = 1; i <= segments; ++i)
1053         {
1054                 // WEAPONTODO (client):
1055                 // In order to do nice fading and pointing on the starting segment, we must always
1056                 // have that drawn as a separate triangle... However, that is difficult to do when
1057                 // keeping in mind the above problems and also optimizing the amount of segments
1058                 // drawn on screen at any given time. (Automatic beam quality scaling, essentially)
1059
1060                 vector new_origin = bezier_quadratic_getpoint(
1061                         start_pos,
1062                         beam_controlpoint,
1063                         beam_endpos,
1064                         i / segments);
1065
1066                 WarpZone_TraceBox_ThroughZone(
1067                         last_origin,
1068                         '0 0 0',
1069                         '0 0 0',
1070                         new_origin,
1071                         MOVE_NORMAL,
1072                         world,
1073                         world,
1074                         Draw_ArcBeam_callback
1075                 );
1076
1077                 // Do all the transforms for warpzones right now, as we already "are" in the post-trace
1078                 // system (if we hit a player, that's always BEHIND the last passed wz).
1079                 last_origin = trace_endpos;
1080                 start_pos = WarpZone_TransformOrigin(WarpZone_trace_transform, start_pos);
1081                 beam_controlpoint = WarpZone_TransformOrigin(WarpZone_trace_transform, beam_controlpoint);
1082                 beam_endpos = WarpZone_TransformOrigin(WarpZone_trace_transform, beam_endpos);
1083                 beamdir = WarpZone_TransformVelocity(WarpZone_trace_transform, beamdir);
1084                 Draw_ArcBeam_callback_last_top = WarpZone_TransformOrigin(WarpZone_trace_transform, Draw_ArcBeam_callback_last_top);
1085                 Draw_ArcBeam_callback_last_bottom = WarpZone_TransformOrigin(WarpZone_trace_transform, Draw_ArcBeam_callback_last_bottom);
1086
1087                 if(trace_fraction < 1) { break; }
1088         }
1089
1090         // visual effects for startpoint and endpoint
1091         if(self.beam_hiteffect)
1092         {
1093                 // FIXME we really should do this on the server so it actually
1094                 // matches gameplay. What this client side stuff is doing is no
1095                 // more than guesswork.
1096                 if((trace_ent || trace_fraction < 1) && !(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT))
1097                 pointparticles(
1098                         self.beam_hiteffect,
1099                         last_origin,
1100                         beamdir * -1,
1101                         frametime * 2
1102                 );
1103         }
1104         if(self.beam_hitlight[0])
1105         {
1106                 adddynamiclight(
1107                         last_origin,
1108                         self.beam_hitlight[0],
1109                         vec3(
1110                                 self.beam_hitlight[1],
1111                                 self.beam_hitlight[2],
1112                                 self.beam_hitlight[3]
1113                         )
1114                 );
1115         }
1116         if(self.beam_muzzleeffect)
1117         {
1118                 pointparticles(
1119                         self.beam_muzzleeffect,
1120                         original_start_pos + wantdir * 20,
1121                         wantdir * 1000,
1122                         frametime * 0.1
1123                 );
1124         }
1125         if(self.beam_muzzlelight[0])
1126         {
1127                 adddynamiclight(
1128                         original_start_pos + wantdir * 20,
1129                         self.beam_muzzlelight[0],
1130                         vec3(
1131                                 self.beam_muzzlelight[1],
1132                                 self.beam_muzzlelight[2],
1133                                 self.beam_muzzlelight[3]
1134                         )
1135                 );
1136         }
1137
1138         // cleanup
1139         Draw_ArcBeam_callback_entity = world;
1140         Draw_ArcBeam_callback_last_thickness = 0;
1141         Draw_ArcBeam_callback_last_top = '0 0 0';
1142         Draw_ArcBeam_callback_last_bottom = '0 0 0';
1143 }
1144
1145 void Remove_ArcBeam()
1146 {SELFPARAM();
1147         remove(self.beam_muzzleentity);
1148         sound(self, CH_SHOTS_SINGLE, SND_Null, VOL_BASE, ATTEN_NORM);
1149 }
1150
1151 NET_HANDLE(ENT_CLIENT_ARC_BEAM, bool isnew)
1152 {
1153         int sf = ReadByte();
1154         entity flash;
1155
1156         if(isnew)
1157         {
1158                 int gunalign = W_GetGunAlignment(world);
1159
1160                 self.beam_shotorigin = arc_shotorigin[gunalign];
1161
1162                 // set other main attributes of the beam
1163                 self.draw = Draw_ArcBeam;
1164                 self.entremove = Remove_ArcBeam;
1165                 self.move_time = time;
1166                 loopsound(self, CH_SHOTS_SINGLE, SND(ARC_LOOP), VOL_BASE, ATTEN_NORM);
1167
1168                 flash = spawn();
1169                 flash.owner = self;
1170                 flash.effects = EF_ADDITIVE | EF_FULLBRIGHT;
1171                 flash.drawmask = MASK_NORMAL;
1172                 flash.solid = SOLID_NOT;
1173                 flash.avelocity_z = 5000;
1174                 setattachment(flash, self, "");
1175                 setorigin(flash, '0 0 0');
1176
1177                 self.beam_muzzleentity = flash;
1178         }
1179         else
1180         {
1181                 flash = self.beam_muzzleentity;
1182         }
1183
1184         if(sf & ARC_SF_SETTINGS) // settings information
1185         {
1186                 self.beam_degreespersegment = ReadShort();
1187                 self.beam_distancepersegment = ReadShort();
1188                 self.beam_maxangle = ReadShort();
1189                 self.beam_range = ReadCoord();
1190                 self.beam_returnspeed = ReadShort();
1191                 self.beam_tightness = (ReadByte() / 10);
1192
1193                 if(ReadByte())
1194                 {
1195                         if(autocvar_chase_active)
1196                                 { self.beam_usevieworigin = 1; }
1197                         else // use view origin
1198                                 { self.beam_usevieworigin = 2; }
1199                 }
1200                 else
1201                 {
1202                         self.beam_usevieworigin = 0;
1203                 }
1204         }
1205
1206         if(!self.beam_usevieworigin)
1207         {
1208                 // self.iflags = IFLAG_ORIGIN | IFLAG_ANGLES | IFLAG_V_ANGLE; // why doesn't this work?
1209                 self.iflags = IFLAG_ORIGIN;
1210
1211                 InterpolateOrigin_Undo();
1212         }
1213
1214         if(sf & ARC_SF_START) // starting location
1215         {
1216                 self.origin_x = ReadCoord();
1217                 self.origin_y = ReadCoord();
1218                 self.origin_z = ReadCoord();
1219         }
1220         else if(self.beam_usevieworigin) // infer the location from player location
1221         {
1222                 if(self.beam_usevieworigin == 2)
1223                 {
1224                         // use view origin
1225                         self.origin = view_origin;
1226                 }
1227                 else
1228                 {
1229                         // use player origin so that third person display still works
1230                         self.origin = getplayerorigin(player_localnum) + ('0 0 1' * getstati(STAT_VIEWHEIGHT));
1231                 }
1232         }
1233
1234         setorigin(self, self.origin);
1235
1236         if(sf & ARC_SF_WANTDIR) // want/aim direction
1237         {
1238                 self.v_angle_x = ReadCoord();
1239                 self.v_angle_y = ReadCoord();
1240                 self.v_angle_z = ReadCoord();
1241         }
1242
1243         if(sf & ARC_SF_BEAMDIR) // beam direction
1244         {
1245                 self.angles_x = ReadCoord();
1246                 self.angles_y = ReadCoord();
1247                 self.angles_z = ReadCoord();
1248         }
1249
1250         if(sf & ARC_SF_BEAMTYPE) // beam type
1251         {
1252                 self.beam_type = ReadByte();
1253                 switch(self.beam_type)
1254                 {
1255                         case ARC_BT_MISS:
1256                         {
1257                                 self.beam_color = '1 1 1';
1258                                 self.beam_alpha = 0.5;
1259                                 self.beam_thickness = 8;
1260                                 self.beam_traileffect = (EFFECT_ARC_BEAM);
1261                                 self.beam_hiteffect = (EFFECT_ARC_LIGHTNING);
1262                                 self.beam_hitlight[0] = 0;
1263                                 self.beam_hitlight[1] = 1;
1264                                 self.beam_hitlight[2] = 1;
1265                                 self.beam_hitlight[3] = 1;
1266                                 self.beam_muzzleeffect = NULL; //(EFFECT_VORTEX_MUZZLEFLASH);
1267                                 self.beam_muzzlelight[0] = 0;
1268                                 self.beam_muzzlelight[1] = 1;
1269                                 self.beam_muzzlelight[2] = 1;
1270                                 self.beam_muzzlelight[3] = 1;
1271                                 if(self.beam_muzzleeffect)
1272                                 {
1273                                         setmodel(flash, MDL_ARC_MUZZLEFLASH);
1274                                         flash.alpha = self.beam_alpha;
1275                                         flash.colormod = self.beam_color;
1276                                         flash.scale = 0.5;
1277                                 }
1278                                 break;
1279                         }
1280                         case ARC_BT_WALL: // grenadelauncher_muzzleflash healray_muzzleflash
1281                         {
1282                                 self.beam_color = '1 1 1';
1283                                 self.beam_alpha = 0.5;
1284                                 self.beam_thickness = 8;
1285                                 self.beam_traileffect = (EFFECT_ARC_BEAM);
1286                                 self.beam_hiteffect = (EFFECT_ARC_LIGHTNING);
1287                                 self.beam_hitlight[0] = 0;
1288                                 self.beam_hitlight[1] = 1;
1289                                 self.beam_hitlight[2] = 1;
1290                                 self.beam_hitlight[3] = 1;
1291                                 self.beam_muzzleeffect = NULL; // (EFFECT_GRENADE_MUZZLEFLASH);
1292                                 self.beam_muzzlelight[0] = 0;
1293                                 self.beam_muzzlelight[1] = 1;
1294                                 self.beam_muzzlelight[2] = 1;
1295                                 self.beam_muzzlelight[3] = 1;
1296                                 self.beam_image = "particles/lgbeam";
1297                                 if(self.beam_muzzleeffect)
1298                                 {
1299                                         setmodel(flash, MDL_ARC_MUZZLEFLASH);
1300                                         flash.alpha = self.beam_alpha;
1301                                         flash.colormod = self.beam_color;
1302                                         flash.scale = 0.5;
1303                                 }
1304                                 break;
1305                         }
1306                         case ARC_BT_HEAL:
1307                         {
1308                                 self.beam_color = '1 1 1';
1309                                 self.beam_alpha = 0.5;
1310                                 self.beam_thickness = 8;
1311                                 self.beam_traileffect = (EFFECT_ARC_BEAM_HEAL);
1312                                 self.beam_hiteffect = (EFFECT_ARC_BEAM_HEAL_IMPACT);
1313                                 self.beam_hitlight[0] = 0;
1314                                 self.beam_hitlight[1] = 1;
1315                                 self.beam_hitlight[2] = 1;
1316                                 self.beam_hitlight[3] = 1;
1317                                 self.beam_muzzleeffect = NULL; //(EFFECT_VORTEX_MUZZLEFLASH);
1318                                 self.beam_muzzlelight[0] = 0;
1319                                 self.beam_muzzlelight[1] = 1;
1320                                 self.beam_muzzlelight[2] = 1;
1321                                 self.beam_muzzlelight[3] = 1;
1322                                 self.beam_image = "particles/lgbeam";
1323                                 if(self.beam_muzzleeffect)
1324                                 {
1325                                         setmodel(flash, MDL_ARC_MUZZLEFLASH);
1326                                         flash.alpha = self.beam_alpha;
1327                                         flash.colormod = self.beam_color;
1328                                         flash.scale = 0.5;
1329                                 }
1330                                 break;
1331                         }
1332                         case ARC_BT_HIT:
1333                         {
1334                                 self.beam_color = '1 1 1';
1335                                 self.beam_alpha = 0.5;
1336                                 self.beam_thickness = 8;
1337                                 self.beam_traileffect = (EFFECT_ARC_BEAM);
1338                                 self.beam_hiteffect = (EFFECT_ARC_LIGHTNING);
1339                                 self.beam_hitlight[0] = 20;
1340                                 self.beam_hitlight[1] = 1;
1341                                 self.beam_hitlight[2] = 0;
1342                                 self.beam_hitlight[3] = 0;
1343                                 self.beam_muzzleeffect = NULL; //(EFFECT_VORTEX_MUZZLEFLASH);
1344                                 self.beam_muzzlelight[0] = 50;
1345                                 self.beam_muzzlelight[1] = 1;
1346                                 self.beam_muzzlelight[2] = 0;
1347                                 self.beam_muzzlelight[3] = 0;
1348                                 self.beam_image = "particles/lgbeam";
1349                                 if(self.beam_muzzleeffect)
1350                                 {
1351                                         setmodel(flash, MDL_ARC_MUZZLEFLASH);
1352                                         flash.alpha = self.beam_alpha;
1353                                         flash.colormod = self.beam_color;
1354                                         flash.scale = 0.5;
1355                                 }
1356                                 break;
1357                         }
1358                         case ARC_BT_BURST_MISS:
1359                         {
1360                                 self.beam_color = '1 1 1';
1361                                 self.beam_alpha = 0.5;
1362                                 self.beam_thickness = 14;
1363                                 self.beam_traileffect = (EFFECT_ARC_BEAM);
1364                                 self.beam_hiteffect = (EFFECT_ARC_LIGHTNING);
1365                                 self.beam_hitlight[0] = 0;
1366                                 self.beam_hitlight[1] = 1;
1367                                 self.beam_hitlight[2] = 1;
1368                                 self.beam_hitlight[3] = 1;
1369                                 self.beam_muzzleeffect = NULL; //(EFFECT_VORTEX_MUZZLEFLASH);
1370                                 self.beam_muzzlelight[0] = 0;
1371                                 self.beam_muzzlelight[1] = 1;
1372                                 self.beam_muzzlelight[2] = 1;
1373                                 self.beam_muzzlelight[3] = 1;
1374                                 self.beam_image = "particles/lgbeam";
1375                                 if(self.beam_muzzleeffect)
1376                                 {
1377                                         setmodel(flash, MDL_ARC_MUZZLEFLASH);
1378                                         flash.alpha = self.beam_alpha;
1379                                         flash.colormod = self.beam_color;
1380                                         flash.scale = 0.5;
1381                                 }
1382                                 break;
1383                         }
1384                         case ARC_BT_BURST_WALL:
1385                         {
1386                                 self.beam_color = '1 1 1';
1387                                 self.beam_alpha = 0.5;
1388                                 self.beam_thickness = 14;
1389                                 self.beam_traileffect = (EFFECT_ARC_BEAM);
1390                                 self.beam_hiteffect = (EFFECT_ARC_LIGHTNING);
1391                                 self.beam_hitlight[0] = 0;
1392                                 self.beam_hitlight[1] = 1;
1393                                 self.beam_hitlight[2] = 1;
1394                                 self.beam_hitlight[3] = 1;
1395                                 self.beam_muzzleeffect = NULL; //(EFFECT_VORTEX_MUZZLEFLASH);
1396                                 self.beam_muzzlelight[0] = 0;
1397                                 self.beam_muzzlelight[1] = 1;
1398                                 self.beam_muzzlelight[2] = 1;
1399                                 self.beam_muzzlelight[3] = 1;
1400                                 self.beam_image = "particles/lgbeam";
1401                                 if(self.beam_muzzleeffect)
1402                                 {
1403                                         setmodel(flash, MDL_ARC_MUZZLEFLASH);
1404                                         flash.alpha = self.beam_alpha;
1405                                         flash.colormod = self.beam_color;
1406                                         flash.scale = 0.5;
1407                                 }
1408                                 break;
1409                         }
1410                         case ARC_BT_BURST_HEAL:
1411                         {
1412                                 self.beam_color = '1 1 1';
1413                                 self.beam_alpha = 0.5;
1414                                 self.beam_thickness = 14;
1415                                 self.beam_traileffect = (EFFECT_ARC_BEAM_HEAL);
1416                                 self.beam_hiteffect = (EFFECT_ARC_BEAM_HEAL_IMPACT2);
1417                                 self.beam_hitlight[0] = 0;
1418                                 self.beam_hitlight[1] = 1;
1419                                 self.beam_hitlight[2] = 1;
1420                                 self.beam_hitlight[3] = 1;
1421                                 self.beam_muzzleeffect = NULL; //(EFFECT_VORTEX_MUZZLEFLASH);
1422                                 self.beam_muzzlelight[0] = 0;
1423                                 self.beam_muzzlelight[1] = 1;
1424                                 self.beam_muzzlelight[2] = 1;
1425                                 self.beam_muzzlelight[3] = 1;
1426                                 self.beam_image = "particles/lgbeam";
1427                                 if(self.beam_muzzleeffect)
1428                                 {
1429                                         setmodel(flash, MDL_ARC_MUZZLEFLASH);
1430                                         flash.alpha = self.beam_alpha;
1431                                         flash.colormod = self.beam_color;
1432                                         flash.scale = 0.5;
1433                                 }
1434                                 break;
1435                         }
1436                         case ARC_BT_BURST_HIT:
1437                         {
1438                                 self.beam_color = '1 1 1';
1439                                 self.beam_alpha = 0.5;
1440                                 self.beam_thickness = 14;
1441                                 self.beam_traileffect = (EFFECT_ARC_BEAM);
1442                                 self.beam_hiteffect = (EFFECT_ARC_LIGHTNING);
1443                                 self.beam_hitlight[0] = 0;
1444                                 self.beam_hitlight[1] = 1;
1445                                 self.beam_hitlight[2] = 1;
1446                                 self.beam_hitlight[3] = 1;
1447                                 self.beam_muzzleeffect = NULL; //(EFFECT_VORTEX_MUZZLEFLASH);
1448                                 self.beam_muzzlelight[0] = 0;
1449                                 self.beam_muzzlelight[1] = 1;
1450                                 self.beam_muzzlelight[2] = 1;
1451                                 self.beam_muzzlelight[3] = 1;
1452                                 self.beam_image = "particles/lgbeam";
1453                                 if(self.beam_muzzleeffect)
1454                                 {
1455                                         setmodel(flash, MDL_ARC_MUZZLEFLASH);
1456                                         flash.alpha = self.beam_alpha;
1457                                         flash.colormod = self.beam_color;
1458                                         flash.scale = 0.5;
1459                                 }
1460                                 break;
1461                         }
1462
1463                         // shouldn't be possible, but lets make it colorful if it does :D
1464                         default:
1465                         {
1466                                 self.beam_color = randomvec();
1467                                 self.beam_alpha = 1;
1468                                 self.beam_thickness = 8;
1469                                 self.beam_traileffect = NULL;
1470                                 self.beam_hiteffect = NULL;
1471                                 self.beam_hitlight[0] = 0;
1472                                 self.beam_hitlight[1] = 1;
1473                                 self.beam_hitlight[2] = 1;
1474                                 self.beam_hitlight[3] = 1;
1475                                 self.beam_muzzleeffect = NULL; //(EFFECT_VORTEX_MUZZLEFLASH);
1476                                 self.beam_muzzlelight[0] = 0;
1477                                 self.beam_muzzlelight[1] = 1;
1478                                 self.beam_muzzlelight[2] = 1;
1479                                 self.beam_muzzlelight[3] = 1;
1480                                 self.beam_image = "particles/lgbeam";
1481                                 if(self.beam_muzzleeffect)
1482                                 {
1483                                         setmodel(flash, MDL_ARC_MUZZLEFLASH);
1484                                         flash.alpha = self.beam_alpha;
1485                                         flash.colormod = self.beam_color;
1486                                         flash.scale = 0.5;
1487                                 }
1488                                 break;
1489                         }
1490                 }
1491         }
1492
1493         if(!self.beam_usevieworigin)
1494         {
1495                 InterpolateOrigin_Note();
1496         }
1497         return true;
1498 }
1499
1500 #endif
1501 #endif