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