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