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