]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/w_arc.qc
Add some comments explaining issues/todo's with the implementation
[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                 // todo: figure out a way so that blendfactor becomes 0 at some point,
209                 // currently self.beam_dir and w_shotdir never really become equal as there is no rounding/snapping point
210                 // printf("blendfactor = %f\n", blendfactor);
211
212                 // network information: beam direction
213                 self.SendFlags |= 8;
214
215                 // calculate how many segments are needed
216                 float max_allowed_segments;
217
218                 if(WEP_CVAR(arc, beam_distancepersegment))
219                         max_allowed_segments = min(ARC_MAX_SEGMENTS, 1 + (vlen(w_shotdir / WEP_CVAR(arc, beam_distancepersegment))));
220                 else
221                         max_allowed_segments = ARC_MAX_SEGMENTS;
222
223                 if(WEP_CVAR(arc, beam_degreespersegment))
224                 {
225                         segments = min( max(1, ( min(angle, WEP_CVAR(arc, beam_maxangle)) / WEP_CVAR(arc, beam_degreespersegment) ) ), max_allowed_segments );
226                 }
227                 else { segments = 1; }
228         }
229         else
230         {
231                 segments = 1;
232         }
233
234         vector beam_endpos_estimate = (w_shotorg + (self.beam_dir * WEP_CVAR(arc, beam_range)));
235
236         float new_beam_type = 0;
237         vector last_origin = w_shotorg;
238         for(i = 1; i <= segments; ++i)
239         {
240                 // WEAPONTODO (server and client):
241                 // Segment blend and distance should probably really be calculated in a better way,
242                 // however I am not sure how to do it properly. There are a few things I have tried,
243                 // but most of them do not work properly due to my lack of understanding regarding
244                 // the mathematics behind them.
245
246                 // Ideally, we should calculate the positions along a perfect curve
247                 // between wantdir and self.beam_dir with an option for depth of arc
248
249                 // Another issue is that (on the client code) we must separate the
250                 // curve into multiple rendered curves when handling warpzones.
251                 
252                 // I can handle this by detecting it for each segment, however that
253                 // is a fairly inefficient method in comparison to having a curved line
254                 // drawing function similar to Draw_CylindricLine that accepts
255                 // top and bottom origins as input, this way there would be no
256                 // overlapping edges when connecting the curved pieces.
257
258                 // WEAPONTODO (client):
259                 // In order to do nice fading and pointing on the starting segment, we must always
260                 // have that drawn as a separate triangle... However, that is difficult to do when
261                 // keeping in mind the above problems and also optimizing the amount of segments
262                 // drawn on screen at any given time. (Automatic beam quality scaling, essentially)
263
264                 // calculate this on every segment to ensure that we always reach the full length of the attack
265                 float segmentblend = bound(0, (i/segments) + WEP_CVAR(arc, beam_tightness), 1);
266                 float segmentdist = vlen(beam_endpos_estimate - last_origin) * (i/segments);
267
268                 vector new_dir = normalize( (w_shotdir * (1 - segmentblend)) + (normalize(beam_endpos_estimate - last_origin) * segmentblend) );
269                 vector new_origin = last_origin + (new_dir * segmentdist);
270
271                 WarpZone_traceline_antilag(
272                         self.owner,
273                         last_origin,
274                         new_origin,
275                         MOVE_NORMAL,
276                         self.owner,
277                         ANTILAG_LATENCY(self.owner)
278                 );
279
280                 float is_player = (trace_ent.classname == "player" || trace_ent.classname == "body" || (trace_ent.flags & FL_MONSTER));
281                 if(trace_ent && trace_ent.takedamage && (is_player || WEP_CVAR(arc, beam_nonplayerdamage)))
282                 {
283                         // calculate our own hit origin as trace_endpos tends to jump around annoyingly (to player origin?)
284                         vector hitorigin = last_origin + (new_dir * segmentdist * trace_fraction);
285
286                         float falloff = ExponentialFalloff(
287                                 WEP_CVAR(arc, beam_falloff_mindist),
288                                 WEP_CVAR(arc, beam_falloff_maxdist),
289                                 WEP_CVAR(arc, beam_falloff_halflifedist),
290                                 vlen(WarpZone_UnTransformOrigin(WarpZone_trace_transform, hitorigin) - w_shotorg)
291                         );
292
293                         if(is_player && SAME_TEAM(self.owner, trace_ent))
294                         {
295                                 // hit a team mate heal them now
296                                 new_beam_type = ARC_BT_HEAL;
297                         }
298                         else
299                         {
300                                 float rootdamage;
301                                 if(is_player)
302                                         rootdamage = WEP_CVAR(arc, beam_damage);
303                                 else
304                                         rootdamage = WEP_CVAR(arc, beam_nonplayerdamage);
305
306                                 if(accuracy_isgooddamage(self.owner, trace_ent))
307                                 {
308                                         accuracy_add(
309                                                 self.owner,
310                                                 WEP_ARC,
311                                                 0,
312                                                 rootdamage * dt * falloff
313                                         );
314                                 }
315
316                                 Damage(
317                                         trace_ent,
318                                         self.owner,
319                                         self.owner,
320                                         rootdamage * dt * falloff,
321                                         WEP_ARC,
322                                         hitorigin,
323                                         WEP_CVAR(arc, beam_force) * new_dir * dt * falloff
324                                 );
325
326                                 new_beam_type = ARC_BT_HIT;
327                         }
328                         break; 
329                 }
330                 else if(trace_fraction != 1)
331                 {
332                         // we collided with geometry
333                         new_beam_type = ARC_BT_WALL;
334                         break;
335                 }
336                 else
337                 {
338                         last_origin = new_origin;
339                 }
340         }
341
342         // if we're bursting, use burst visual effects
343         new_beam_type += burst;
344
345         // network information: beam type
346         if(new_beam_type != self.beam_type)
347         {
348                 self.SendFlags |= 16;
349                 self.beam_type = new_beam_type;
350         }
351
352         self.owner.lg_fire_prev = time;
353         self.nextthink = time;
354 }
355
356 // Attack functions ========================= 
357 void W_Arc_Beam(void)
358 {
359         print("W_Arc_Beam();\n");
360         // only play fire sound if 1 sec has passed since player let go the fire button
361         if(time - self.lg_fire_prev > 1)
362                 sound(self, CH_WEAPON_A, "weapons/lgbeam_fire.wav", VOL_BASE, ATTN_NORM);
363
364         entity beam, oldself;
365
366         self.arc_beam = beam = spawn();
367         beam.classname = "W_Arc_Beam";
368         beam.solid = SOLID_NOT;
369         beam.think = W_Arc_Beam_Think;
370         beam.owner = self;
371         beam.movetype = MOVETYPE_NONE;
372         beam.shot_spread = 1;
373         beam.bot_dodge = TRUE;
374         beam.bot_dodgerating = WEP_CVAR(arc, beam_damage);
375         Net_LinkEntity(beam, FALSE, 0, W_Arc_Beam_Send);
376
377         oldself = self;
378         self = beam;
379         self.think();
380         self = oldself;
381 }
382
383 float W_Arc(float req)
384 {
385         switch(req)
386         {
387                 case WR_AIM:
388                 {
389                         if(WEP_CVAR(arc, beam_botaimspeed))
390                                 self.BUTTON_ATCK = bot_aim(WEP_CVAR(arc, beam_botaimspeed), 0, WEP_CVAR(arc, beam_botaimlifetime), FALSE);
391                         else
392                                 self.BUTTON_ATCK = bot_aim(1000000, 0, 0.001, FALSE);
393                         return TRUE;
394                 }
395                 case WR_THINK:
396                 {
397                         if(self.BUTTON_ATCK)
398                         {
399                                 if(self.BUTTON_ATCK_prev) // TODO: Find another way to implement this!
400                                         /*if(self.animstate_startframe == self.anim_shoot_x && self.animstate_numframes == self.anim_shoot_y)
401                                                 weapon_thinkf(WFRAME_DONTCHANGE, autocvar_g_balance_arc_primary_animtime, w_ready);
402                                         else*/
403                                                 weapon_thinkf(WFRAME_FIRE1, WEP_CVAR(arc, beam_animtime), w_ready);
404                                 
405                                 if(weapon_prepareattack(0, 0))
406                                 {
407                                         if((!self.arc_beam) || wasfreed(self.arc_beam))
408                                                 W_Arc_Beam();
409                                         
410                                         if(!self.BUTTON_ATCK_prev)
411                                         {
412                                                 weapon_thinkf(WFRAME_FIRE1, WEP_CVAR(arc, beam_animtime), w_ready);
413                                                 self.BUTTON_ATCK_prev = 1;
414                                         }
415                                 }
416                         } 
417                         else // todo
418                         {
419                                 if(self.BUTTON_ATCK_prev != 0)
420                                 {
421                                         weapon_thinkf(WFRAME_FIRE1, WEP_CVAR(arc, beam_animtime), w_ready);
422                                         ATTACK_FINISHED(self) = time + WEP_CVAR(arc, beam_refire) * W_WeaponRateFactor();
423                                 }
424                                 self.BUTTON_ATCK_prev = 0;
425                         }
426
427                         //if(self.BUTTON_ATCK2)
428                                 //if(weapon_prepareattack(1, autocvar_g_balance_arc_secondary_refire))
429                                 //{
430                                 //      W_Arc_Attack2();
431                                 //      self.arc_count = autocvar_g_balance_arc_secondary_count;
432                                 //      weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_arc_secondary_animtime, w_arc_checkattack);
433                                 //      self.arc_secondarytime = time + autocvar_g_balance_arc_secondary_refire2 * W_WeaponRateFactor();
434                                 //}
435                                 
436                         return TRUE;
437                 }
438                 case WR_INIT:
439                 {
440                         precache_model("models/weapons/g_arc.md3");
441                         precache_model("models/weapons/v_arc.md3");
442                         precache_model("models/weapons/h_arc.iqm");
443                         //precache_sound("weapons/arc_bounce.wav");
444                         precache_sound("weapons/arc_fire.wav");
445                         precache_sound("weapons/arc_fire2.wav");
446                         precache_sound("weapons/arc_impact.wav");
447                         //precache_sound("weapons/arc_impact_combo.wav");
448                         //precache_sound("weapons/W_Arc_Beam_fire.wav");
449                         return TRUE;
450                 }
451                 case WR_CHECKAMMO1:
452                 {
453                         return !WEP_CVAR(arc, beam_ammo) || (self.WEP_AMMO(ARC) > 0);
454                 }
455                 case WR_CHECKAMMO2:
456                 {
457                         //return self.WEP_AMMO(ARC) >= WEP_CVAR_SEC(arc, ammo);
458                         return TRUE;
459                 }
460                 case WR_CONFIG:
461                 {
462                         ARC_SETTINGS(WEP_CONFIG_WRITE_CVARS, WEP_CONFIG_WRITE_PROPS)
463                         return TRUE;
464                 }
465                 case WR_KILLMESSAGE:
466                 {
467                         if(w_deathtype & HITTYPE_SECONDARY)
468                         {
469                                 return WEAPON_ELECTRO_MURDER_ORBS;
470                         }
471                         else
472                         {
473                                 if(w_deathtype & HITTYPE_BOUNCE)
474                                         return WEAPON_ELECTRO_MURDER_COMBO;
475                                 else
476                                         return WEAPON_ELECTRO_MURDER_BOLT;
477                         }
478                 }
479                 case WR_RESETPLAYER:
480                 {
481                         //self.arc_secondarytime = time;
482                         return TRUE;
483                 }
484         }
485         return FALSE;
486 }
487
488 void ArcInit(void)
489 {
490         WEP_ACTION(WEP_ARC, WR_INIT);
491         arc_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ARC), FALSE, FALSE, 1);
492         arc_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ARC), FALSE, FALSE, 2);
493         arc_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ARC), FALSE, FALSE, 3);
494         arc_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ARC), FALSE, FALSE, 4);
495         ARC_SETTINGS(WEP_SKIP_CVAR, WEP_SET_PROP)
496 }
497 #endif
498 #ifdef CSQC
499 float W_Arc(float req)
500 {
501         switch(req)
502         {
503                 case WR_IMPACTEFFECT:
504                 {
505                         vector org2;
506                         org2 = w_org + w_backoff * 6;
507                         
508                         if(w_deathtype & HITTYPE_SECONDARY)
509                         {
510                                 pointparticles(particleeffectnum("arc_ballexplode"), org2, '0 0 0', 1);
511                                 if(!w_issilent)
512                                         sound(self, CH_SHOTS, "weapons/arc_impact.wav", VOL_BASE, ATTN_NORM);
513                         }
514                         else
515                         {
516                                 pointparticles(particleeffectnum("arc_impact"), org2, '0 0 0', 1);
517                                 if(!w_issilent)
518                                         sound(self, CH_SHOTS, "weapons/arc_impact.wav", VOL_BASE, ATTN_NORM);
519                         }
520                         
521                         return TRUE;
522                 }
523                 case WR_INIT:
524                 {
525                         precache_sound("weapons/arc_impact.wav");
526                         precache_sound("weapons/arc_impact_combo.wav");
527                         return TRUE;
528                 }
529                 case WR_ZOOMRETICLE:
530                 {
531                         // no weapon specific image for this weapon
532                         return FALSE;
533                 }
534         }
535         return FALSE;
536 }
537 #endif
538 #endif