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