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