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