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