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