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