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