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