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