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