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