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