]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/w_arc.qc
More cleanup and comments
[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 */ "hlac",
11 /* simplemdl */ "foobar",
12 /* crosshair */ "gfx/crosshairhlac 0.7",
13 /* wepimg    */ "weaponhlac",
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_maxangle) \
32         w_cvar(id, sn, NONE, beam_nonplayerdamage) \
33         w_cvar(id, sn, NONE, beam_range) \
34         w_cvar(id, sn, NONE, beam_refire) \
35         w_cvar(id, sn, NONE, beam_returnspeed) \
36         w_cvar(id, sn, NONE, beam_tightness) \
37         w_prop(id, sn, float,  switchdelay_raise, switchdelay_raise) \
38         w_prop(id, sn, float,  switchdelay_drop, switchdelay_drop) \
39         w_prop(id, sn, string, weaponreplace, weaponreplace) \
40         w_prop(id, sn, float,  weaponstart, weaponstart) \
41         w_prop(id, sn, float,  weaponstartoverride, weaponstartoverride) \
42         w_prop(id, sn, float,  weaponthrowable, weaponthrowable)
43
44 #ifndef MENUQC
45 vector arc_shotorigin[4];
46 .vector beam_start;
47 .vector beam_dir;
48 .vector beam_wantdir;
49 .float beam_type;
50 #define ARC_BT_MISS        0
51 #define ARC_BT_WALL        1
52 #define ARC_BT_HEAL        2
53 #define ARC_BT_HIT         3
54 #define ARC_BT_BURST_MISS  10
55 #define ARC_BT_BURST_WALL  11
56 #define ARC_BT_BURST_HEAL  12
57 #define ARC_BT_BURST_HIT   13
58 #define ARC_BT_BURSTMASK   10
59 #endif
60 #ifdef SVQC
61 #define ARC_MAX_SEGMENTS 20
62 ARC_SETTINGS(WEP_ADD_CVAR, WEP_ADD_PROP)
63 void ArcInit(void);
64 .entity arc_beam; // used for beam
65 .float BUTTON_ATCK_prev; // for better animation control
66 .float lg_fire_prev; // for better animation control
67 .float beam_initialized;
68 #endif
69 #else
70 #ifdef SVQC
71 void spawnfunc_weapon_arc(void) { weapon_defaultspawnfunc(WEP_ARC); }
72
73 float W_Arc_Beam_Send(entity to, float sf)
74 {
75         WriteByte(MSG_ENTITY, ENT_CLIENT_ARC_BEAM);
76
77         // Truncate information when this beam is displayed to the owner client
78         // - The owner client has no use for beam start position or directions,
79         //    it always figures this information out for itself with csqc code.
80         // - Spectating the owner also truncates this information.
81         float drawlocal = ((to == self.owner) || ((to.enemy == self.owner) && IS_SPEC(to)));
82         if(drawlocal)
83         {
84                 #if 0
85                 sf &= ~2;
86                 sf &= ~4;
87                 sf &= ~8;
88                 #else
89                 sf &= ~14;
90                 #endif
91         }
92
93         WriteByte(MSG_ENTITY, sf);
94
95         if(sf & 1) // settings information
96         {
97                 WriteShort(MSG_ENTITY, WEP_CVAR(arc, beam_maxangle));
98                 WriteCoord(MSG_ENTITY, WEP_CVAR(arc, beam_range));
99                 WriteShort(MSG_ENTITY, WEP_CVAR(arc, beam_returnspeed));
100                 WriteByte(MSG_ENTITY, WEP_CVAR(arc, beam_tightness) * 10);
101
102                 WriteByte(MSG_ENTITY, drawlocal);
103         }
104         if(sf & 2) // starting location
105         {
106                 WriteCoord(MSG_ENTITY, self.beam_start_x);
107                 WriteCoord(MSG_ENTITY, self.beam_start_y);
108                 WriteCoord(MSG_ENTITY, self.beam_start_z);
109         }
110         if(sf & 4) // want/aim direction
111         {
112                 WriteCoord(MSG_ENTITY, self.beam_wantdir_x);
113                 WriteCoord(MSG_ENTITY, self.beam_wantdir_y);
114                 WriteCoord(MSG_ENTITY, self.beam_wantdir_z);
115         }
116         if(sf & 8) // beam direction
117         {
118                 WriteCoord(MSG_ENTITY, self.beam_dir_x);
119                 WriteCoord(MSG_ENTITY, self.beam_dir_y);
120                 WriteCoord(MSG_ENTITY, self.beam_dir_z);
121         }
122         if(sf & 16) // beam type
123         {
124                 WriteByte(MSG_ENTITY, self.beam_type);
125         }
126
127         return TRUE;
128 }
129
130 void W_Arc_Beam_Think(void)
131 {
132         float i, burst = 0;
133         if(self != self.owner.arc_beam)
134         {
135                 remove(self);
136                 return;
137         }
138         if((self.owner.WEP_AMMO(ARC) <= 0 && !(self.owner.items & IT_UNLIMITED_WEAPON_AMMO)) || self.owner.deadflag != DEAD_NO || !self.owner.BUTTON_ATCK || self.owner.freezetag_frozen)
139         {
140                 if(self == self.owner.arc_beam) { self.owner.arc_beam = world; } // is this needed? I thought this is changed to world when removed ANYWAY
141                 remove(self);
142                 return;
143         }
144
145         if(self.owner.BUTTON_ATCK2)
146         {
147                 burst = ARC_BT_BURSTMASK;
148         }
149
150         // decrease ammo // todo: support burst ammo
151         float dt = frametime;
152         if(!(self.owner.items & IT_UNLIMITED_WEAPON_AMMO))
153         {
154                 if(WEP_CVAR(arc, beam_ammo))
155                 {
156                         dt = min(dt, self.owner.WEP_AMMO(ARC) / WEP_CVAR(arc, beam_ammo));
157                         self.owner.WEP_AMMO(ARC) = max(0, self.owner.WEP_AMMO(ARC) - WEP_CVAR(arc, beam_ammo) * frametime);
158                 }
159         }
160
161         makevectors(self.owner.v_angle);
162
163         W_SetupShot_Range(self.owner, TRUE, 0, "", 0, WEP_CVAR(arc, beam_damage) * dt, WEP_CVAR(arc, beam_range));
164
165         // network information: shot origin and want/aim direction
166         if(self.beam_start != w_shotorg)
167         {
168                 self.SendFlags |= 2;
169                 self.beam_start = w_shotorg;
170         }
171         if(self.beam_wantdir != w_shotdir)
172         {
173                 self.SendFlags |= 4;
174                 self.beam_wantdir = w_shotdir;
175         }
176
177         if(!self.beam_initialized)
178         {
179                 #ifdef ARC_DEBUG
180                 for(i = 0; i < ARC_MAX_SEGMENTS; ++i)
181                         self.lg_ents[i] = spawn();
182                 #endif
183                 
184                 self.beam_dir = w_shotdir;
185                 self.beam_initialized = TRUE;
186         }
187
188         float segments; 
189         if(self.beam_dir != w_shotdir)
190         {
191                 float angle = ceil(vlen(w_shotdir - self.beam_dir) * RAD2DEG);
192                 float anglelimit;
193                 if(angle && (angle > WEP_CVAR(arc, beam_maxangle)))
194                 {
195                         // if the angle is greater than maxangle, force the blendfactor to make this the maximum factor
196                         anglelimit = min(WEP_CVAR(arc, beam_maxangle) / angle, 1);
197                 }
198                 else
199                 {
200                         // the radius is not too far yet, no worries :D
201                         anglelimit = 1;
202                 }
203
204                 // calculate how much we're going to move the end of the beam to the want position
205                 float blendfactor = bound(0, anglelimit * (1 - (WEP_CVAR(arc, beam_returnspeed) * dt)), 1);
206                 self.beam_dir = normalize((w_shotdir * (1 - blendfactor)) + (self.beam_dir * blendfactor));
207
208                 // WEAPONTODO (server and client):
209                 // blendfactor never actually becomes 0 in this situation, which is a problem
210                 // regarding precision... this means that self.beam_dir and w_shotdir approach
211                 // eachother, however they never actually become the same value with this method.
212                 
213                 // Perhaps we should do some form of rounding/snapping?
214
215                 // printf("blendfactor = %f\n", blendfactor);
216
217                 // network information: beam direction
218                 self.SendFlags |= 8;
219
220                 // calculate how many segments are needed
221                 float max_allowed_segments;
222
223                 if(WEP_CVAR(arc, beam_distancepersegment))
224                         max_allowed_segments = min(ARC_MAX_SEGMENTS, 1 + (vlen(w_shotdir / WEP_CVAR(arc, beam_distancepersegment))));
225                 else
226                         max_allowed_segments = ARC_MAX_SEGMENTS;
227
228                 if(WEP_CVAR(arc, beam_degreespersegment))
229                 {
230                         segments = min( max(1, ( min(angle, WEP_CVAR(arc, beam_maxangle)) / WEP_CVAR(arc, beam_degreespersegment) ) ), max_allowed_segments );
231                 }
232                 else
233                 {
234                         segments = 1;
235                 }
236         }
237         else
238         {
239                 segments = 1;
240         }
241
242         vector beam_endpos_estimate = (w_shotorg + (self.beam_dir * WEP_CVAR(arc, beam_range)));
243
244         float new_beam_type = 0;
245         vector last_origin = w_shotorg;
246         for(i = 1; i <= segments; ++i)
247         {
248                 // WEAPONTODO (server and client):
249                 // Segment blend and distance should probably really be calculated in a better way,
250                 // however I am not sure how to do it properly. There are a few things I have tried,
251                 // but most of them do not work properly due to my lack of understanding regarding
252                 // the mathematics behind them.
253
254                 // Ideally, we should calculate the positions along a perfect curve
255                 // between wantdir and self.beam_dir with an option for depth of arc
256
257                 // Another issue is that (on the client code) we must separate the
258                 // curve into multiple rendered curves when handling warpzones.
259                 
260                 // I can handle this by detecting it for each segment, however that
261                 // is a fairly inefficient method in comparison to having a curved line
262                 // drawing function similar to Draw_CylindricLine that accepts
263                 // top and bottom origins as input, this way there would be no
264                 // overlapping edges when connecting the curved pieces.
265
266                 // WEAPONTODO (client):
267                 // In order to do nice fading and pointing on the starting segment, we must always
268                 // have that drawn as a separate triangle... However, that is difficult to do when
269                 // keeping in mind the above problems and also optimizing the amount of segments
270                 // drawn on screen at any given time. (Automatic beam quality scaling, essentially)
271
272                 // calculate this on every segment to ensure that we always reach the full length of the attack
273                 float segmentblend = bound(0, (i/segments) + WEP_CVAR(arc, beam_tightness), 1);
274                 float segmentdist = vlen(beam_endpos_estimate - last_origin) * (i/segments);
275
276                 vector new_dir = normalize( (w_shotdir * (1 - segmentblend)) + (normalize(beam_endpos_estimate - last_origin) * segmentblend) );
277                 vector new_origin = last_origin + (new_dir * segmentdist);
278
279                 WarpZone_traceline_antilag(
280                         self.owner,
281                         last_origin,
282                         new_origin,
283                         MOVE_NORMAL,
284                         self.owner,
285                         ANTILAG_LATENCY(self.owner)
286                 );
287
288                 float is_player = (trace_ent.classname == "player" || trace_ent.classname == "body" || (trace_ent.flags & FL_MONSTER));
289                 if(trace_ent && trace_ent.takedamage && (is_player || WEP_CVAR(arc, beam_nonplayerdamage)))
290                 {
291                         // calculate our own hit origin as trace_endpos tends to jump around annoyingly (to player origin?)
292                         vector hitorigin = last_origin + (new_dir * segmentdist * trace_fraction);
293
294                         float falloff = ExponentialFalloff(
295                                 WEP_CVAR(arc, beam_falloff_mindist),
296                                 WEP_CVAR(arc, beam_falloff_maxdist),
297                                 WEP_CVAR(arc, beam_falloff_halflifedist),
298                                 vlen(WarpZone_UnTransformOrigin(WarpZone_trace_transform, hitorigin) - w_shotorg)
299                         );
300
301                         if(is_player && SAME_TEAM(self.owner, trace_ent))
302                         {
303                                 // hit a team mate heal them now
304                                 new_beam_type = ARC_BT_HEAL;
305                         }
306                         else
307                         {
308                                 float rootdamage;
309                                 if(is_player)
310                                         rootdamage = WEP_CVAR(arc, beam_damage);
311                                 else
312                                         rootdamage = WEP_CVAR(arc, beam_nonplayerdamage);
313
314                                 if(accuracy_isgooddamage(self.owner, trace_ent))
315                                 {
316                                         accuracy_add(
317                                                 self.owner,
318                                                 WEP_ARC,
319                                                 0,
320                                                 rootdamage * dt * falloff
321                                         );
322                                 }
323
324                                 Damage(
325                                         trace_ent,
326                                         self.owner,
327                                         self.owner,
328                                         rootdamage * dt * falloff,
329                                         WEP_ARC,
330                                         hitorigin,
331                                         WEP_CVAR(arc, beam_force) * new_dir * dt * falloff
332                                 );
333
334                                 new_beam_type = ARC_BT_HIT;
335                         }
336                         break; 
337                 }
338                 else if(trace_fraction != 1)
339                 {
340                         // we collided with geometry
341                         new_beam_type = ARC_BT_WALL;
342                         break;
343                 }
344                 else
345                 {
346                         last_origin = new_origin;
347                 }
348         }
349
350         // if we're bursting, use burst visual effects
351         new_beam_type += burst;
352
353         // network information: beam type
354         if(new_beam_type != self.beam_type)
355         {
356                 self.SendFlags |= 16;
357                 self.beam_type = new_beam_type;
358         }
359
360         self.owner.lg_fire_prev = time;
361         self.nextthink = time;
362 }
363
364 // Attack functions ========================= 
365 void W_Arc_Beam(void)
366 {
367         print("W_Arc_Beam();\n");
368         // only play fire sound if 1 sec has passed since player let go the fire button
369         if(time - self.lg_fire_prev > 1)
370                 sound(self, CH_WEAPON_A, "weapons/lgbeam_fire.wav", VOL_BASE, ATTN_NORM);
371
372         entity beam, oldself;
373
374         self.arc_beam = beam = spawn();
375         beam.classname = "W_Arc_Beam";
376         beam.solid = SOLID_NOT;
377         beam.think = W_Arc_Beam_Think;
378         beam.owner = self;
379         beam.movetype = MOVETYPE_NONE;
380         beam.shot_spread = 1;
381         beam.bot_dodge = TRUE;
382         beam.bot_dodgerating = WEP_CVAR(arc, beam_damage);
383         Net_LinkEntity(beam, FALSE, 0, W_Arc_Beam_Send);
384
385         oldself = self;
386         self = beam;
387         self.think();
388         self = oldself;
389 }
390
391 float W_Arc(float req)
392 {
393         switch(req)
394         {
395                 case WR_AIM:
396                 {
397                         if(WEP_CVAR(arc, beam_botaimspeed))
398                                 self.BUTTON_ATCK = bot_aim(WEP_CVAR(arc, beam_botaimspeed), 0, WEP_CVAR(arc, beam_botaimlifetime), FALSE);
399                         else
400                                 self.BUTTON_ATCK = bot_aim(1000000, 0, 0.001, FALSE);
401                         return TRUE;
402                 }
403                 case WR_THINK:
404                 {
405                         if(self.BUTTON_ATCK)
406                         {
407                                 if(self.BUTTON_ATCK_prev) // TODO: Find another way to implement this!
408                                         /*if(self.animstate_startframe == self.anim_shoot_x && self.animstate_numframes == self.anim_shoot_y)
409                                                 weapon_thinkf(WFRAME_DONTCHANGE, autocvar_g_balance_arc_primary_animtime, w_ready);
410                                         else*/
411                                                 weapon_thinkf(WFRAME_FIRE1, WEP_CVAR(arc, beam_animtime), w_ready);
412                                 
413                                 if(weapon_prepareattack(0, 0))
414                                 {
415                                         if((!self.arc_beam) || wasfreed(self.arc_beam))
416                                                 W_Arc_Beam();
417                                         
418                                         if(!self.BUTTON_ATCK_prev)
419                                         {
420                                                 weapon_thinkf(WFRAME_FIRE1, WEP_CVAR(arc, beam_animtime), w_ready);
421                                                 self.BUTTON_ATCK_prev = 1;
422                                         }
423                                 }
424                         } 
425                         else // todo
426                         {
427                                 if(self.BUTTON_ATCK_prev != 0)
428                                 {
429                                         weapon_thinkf(WFRAME_FIRE1, WEP_CVAR(arc, beam_animtime), w_ready);
430                                         ATTACK_FINISHED(self) = time + WEP_CVAR(arc, beam_refire) * W_WeaponRateFactor();
431                                 }
432                                 self.BUTTON_ATCK_prev = 0;
433                         }
434
435                         //if(self.BUTTON_ATCK2)
436                                 //if(weapon_prepareattack(1, autocvar_g_balance_arc_secondary_refire))
437                                 //{
438                                 //      W_Arc_Attack2();
439                                 //      self.arc_count = autocvar_g_balance_arc_secondary_count;
440                                 //      weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_arc_secondary_animtime, w_arc_checkattack);
441                                 //      self.arc_secondarytime = time + autocvar_g_balance_arc_secondary_refire2 * W_WeaponRateFactor();
442                                 //}
443                                 
444                         return TRUE;
445                 }
446                 case WR_INIT:
447                 {
448                         precache_model("models/weapons/g_arc.md3");
449                         precache_model("models/weapons/v_arc.md3");
450                         precache_model("models/weapons/h_arc.iqm");
451                         //precache_sound("weapons/arc_bounce.wav");
452                         precache_sound("weapons/arc_fire.wav");
453                         precache_sound("weapons/arc_fire2.wav");
454                         precache_sound("weapons/arc_impact.wav");
455                         //precache_sound("weapons/arc_impact_combo.wav");
456                         //precache_sound("weapons/W_Arc_Beam_fire.wav");
457                         return TRUE;
458                 }
459                 case WR_CHECKAMMO1:
460                 {
461                         return !WEP_CVAR(arc, beam_ammo) || (self.WEP_AMMO(ARC) > 0);
462                 }
463                 case WR_CHECKAMMO2:
464                 {
465                         //return self.WEP_AMMO(ARC) >= WEP_CVAR_SEC(arc, ammo);
466                         return TRUE;
467                 }
468                 case WR_CONFIG:
469                 {
470                         ARC_SETTINGS(WEP_CONFIG_WRITE_CVARS, WEP_CONFIG_WRITE_PROPS)
471                         return TRUE;
472                 }
473                 case WR_KILLMESSAGE:
474                 {
475                         if(w_deathtype & HITTYPE_SECONDARY)
476                         {
477                                 return WEAPON_ELECTRO_MURDER_ORBS;
478                         }
479                         else
480                         {
481                                 if(w_deathtype & HITTYPE_BOUNCE)
482                                         return WEAPON_ELECTRO_MURDER_COMBO;
483                                 else
484                                         return WEAPON_ELECTRO_MURDER_BOLT;
485                         }
486                 }
487                 case WR_RESETPLAYER:
488                 {
489                         //self.arc_secondarytime = time;
490                         return TRUE;
491                 }
492         }
493         return FALSE;
494 }
495
496 void ArcInit(void)
497 {
498         WEP_ACTION(WEP_ARC, WR_INIT);
499         arc_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ARC), FALSE, FALSE, 1);
500         arc_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ARC), FALSE, FALSE, 2);
501         arc_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ARC), FALSE, FALSE, 3);
502         arc_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ARC), FALSE, FALSE, 4);
503         ARC_SETTINGS(WEP_SKIP_CVAR, WEP_SET_PROP)
504 }
505 #endif
506 #ifdef CSQC
507 float W_Arc(float req)
508 {
509         switch(req)
510         {
511                 case WR_IMPACTEFFECT:
512                 {
513                         vector org2;
514                         org2 = w_org + w_backoff * 6;
515                         
516                         if(w_deathtype & HITTYPE_SECONDARY)
517                         {
518                                 pointparticles(particleeffectnum("arc_ballexplode"), org2, '0 0 0', 1);
519                                 if(!w_issilent)
520                                         sound(self, CH_SHOTS, "weapons/arc_impact.wav", VOL_BASE, ATTN_NORM);
521                         }
522                         else
523                         {
524                                 pointparticles(particleeffectnum("arc_impact"), org2, '0 0 0', 1);
525                                 if(!w_issilent)
526                                         sound(self, CH_SHOTS, "weapons/arc_impact.wav", VOL_BASE, ATTN_NORM);
527                         }
528                         
529                         return TRUE;
530                 }
531                 case WR_INIT:
532                 {
533                         precache_sound("weapons/arc_impact.wav");
534                         precache_sound("weapons/arc_impact_combo.wav");
535                         return TRUE;
536                 }
537                 case WR_ZOOMRETICLE:
538                 {
539                         // no weapon specific image for this weapon
540                         return FALSE;
541                 }
542         }
543         return FALSE;
544 }
545 #endif
546 #endif