]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/w_arc.qc
332b98a991b9617dd9a334db43caf638e9984483
[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, BOTH, ammo) \
21         w_cvar(id, sn, PRI,  animtime) \
22         w_cvar(id, sn, PRI,  botaimspeed) \
23         w_cvar(id, sn, PRI,  botaimlifetime) \
24         w_cvar(id, sn, PRI,  damage) \
25         w_cvar(id, sn, PRI,  degreespersegment) \
26         w_cvar(id, sn, PRI,  distancepersegment) \
27         w_cvar(id, sn, PRI,  falloff_halflifedist) \
28         w_cvar(id, sn, PRI,  falloff_maxdist) \
29         w_cvar(id, sn, PRI,  falloff_mindist) \
30         w_cvar(id, sn, PRI,  force) \
31         w_cvar(id, sn, PRI,  maxangle) \
32         w_cvar(id, sn, PRI,  range) \
33         w_cvar(id, sn, PRI,  refire) \
34         w_cvar(id, sn, PRI,  returnspeed) \
35         w_prop(id, sn, float,  switchdelay_raise, switchdelay_raise) \
36         w_prop(id, sn, float,  switchdelay_drop, switchdelay_drop) \
37         w_prop(id, sn, string, weaponreplace, weaponreplace) \
38         w_prop(id, sn, float,  weaponstart, weaponstart) \
39         w_prop(id, sn, float,  weaponstartoverride, weaponstartoverride) \
40         w_prop(id, sn, float,  weaponthrowable, weaponthrowable)
41
42 #ifndef MENUQC
43 vector arc_shotorigin[4];
44 #define ARC_BT_WALL 1
45 #define ARC_BT_HEAL 2
46 #define ARC_BT_ENEMY 3
47 #endif
48 #ifdef SVQC
49 #define ARC_MAX_SEGMENTS 20
50 ARC_SETTINGS(WEP_ADD_CVAR, WEP_ADD_PROP)
51 void ArcInit(void);
52 .entity arc_beam; // used for beam
53 .float BUTTON_ATCK_prev; // for better animation control
54 .float lg_fire_prev; // for better animation control
55 #define ARC_DEBUG
56 #ifdef ARC_DEBUG
57 .entity lg_ents[ARC_MAX_SEGMENTS]; // debug
58 #endif
59 .vector beam_dir;
60 .vector beam_wantdir;
61 .float beam_initialized;
62 .float beam_type;
63 #endif
64 #else
65 #ifdef SVQC
66 void spawnfunc_weapon_arc(void) { weapon_defaultspawnfunc(WEP_ARC); }
67
68 float W_Arc_Beam_Send(entity to, float sf)
69 {
70         WriteByte(MSG_ENTITY, ENT_CLIENT_ARC_BEAM);
71         sf = sf & 0x7F;
72         if(sound_allowed(MSG_BROADCAST, self.owner))
73                 sf |= 0x80;
74         WriteByte(MSG_ENTITY, sf);
75         if(sf & 1) // main information
76         {
77                 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
78                 WriteCoord(MSG_ENTITY, WEP_CVAR_PRI(arc, range));
79         }
80         if(sf & 2) // want/aim direction
81         {
82                 WriteCoord(MSG_ENTITY, self.beam_wantdir_x);
83                 WriteCoord(MSG_ENTITY, self.beam_wantdir_y);
84                 WriteCoord(MSG_ENTITY, self.beam_wantdir_z);
85         }
86         if(sf & 4) // beam direction
87         {
88                 WriteCoord(MSG_ENTITY, self.beam_dir_x);
89                 WriteCoord(MSG_ENTITY, self.beam_dir_y);
90                 WriteCoord(MSG_ENTITY, self.beam_dir_z);
91         }
92         if(sf & 8) // beam type
93         {
94                 WriteByte(MSG_ENTITY, self.beam_type);
95         }
96         return TRUE;
97 }
98 void W_Arc_Beam_Think(void)
99 {
100         float i;
101         if(self != self.owner.arc_beam)
102         {
103                 #ifdef ARC_DEBUG
104                 print("W_Arc_Beam_Think(): EXPIRING BEAM #1\n");
105                 #endif
106                 remove(self);
107                 return;
108         }
109         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)
110         {
111                 if(self == self.owner.arc_beam) { self.owner.arc_beam = world; } // is this needed? I thought this is changed to world when removed ANYWAY
112                 #ifdef ARC_DEBUG
113                 if(self.lg_ents[0])
114                 {
115                         for(i = 0; i < ARC_MAX_SEGMENTS; ++i)
116                                 remove(self.lg_ents[i]);
117                 }
118                 print("W_Arc_Beam_Think(): EXPIRING BEAM #2\n");
119                 #endif
120                 remove(self);
121                 return;
122         }
123
124         // decrease ammo
125         float dt = frametime;
126         if(!(self.owner.items & IT_UNLIMITED_WEAPON_AMMO))
127         {
128                 if(WEP_CVAR_PRI(arc, ammo))
129                 {
130                         dt = min(dt, self.owner.WEP_AMMO(ARC) / WEP_CVAR_PRI(arc, ammo));
131                         self.owner.WEP_AMMO(ARC) = max(0, self.owner.WEP_AMMO(ARC) - WEP_CVAR_PRI(arc, ammo) * frametime);
132                 }
133         }
134
135         makevectors(self.owner.v_angle);
136
137         W_SetupShot_Range(self.owner, TRUE, 0, "", 0, WEP_CVAR_PRI(arc, damage) * dt, WEP_CVAR_PRI(arc, range));
138
139         // network information: want/aim direction
140         if(self.beam_wantdir != w_shotdir)
141         {
142                 self.SendFlags |= 2;
143                 self.beam_wantdir = w_shotdir;
144         }
145
146         if(!self.beam_initialized)
147         {
148                 #ifdef ARC_DEBUG
149                 for(i = 0; i < ARC_MAX_SEGMENTS; ++i)
150                         self.lg_ents[i] = spawn();
151                 #endif
152                 
153                 self.beam_dir = w_shotdir;
154                 self.beam_initialized = TRUE;
155         }
156
157         float segments; 
158         if(self.beam_dir != w_shotdir)
159         {
160                 float angle = ceil(vlen(w_shotdir - self.beam_dir) * RAD2DEG);
161                 float anglelimit;
162                 if(angle && (angle > WEP_CVAR_PRI(arc, maxangle)))
163                 {
164                         // if the angle is greater than maxangle, force the blendfactor to make this the maximum factor
165                         #ifdef ARC_DEBUG
166                         printf("Correcting max angle: %f %f\n", angle, WEP_CVAR_PRI(arc, distancepersegment));
167                         #endif
168                         anglelimit = min(WEP_CVAR_PRI(arc, maxangle) / angle, 1);
169                 }
170                 else
171                 {
172                         // the radius is not too far yet, no worries :D
173                         anglelimit = 1;
174                 }
175
176                 // calculate how much we're going to move the end of the beam to the want position
177                 float blendfactor = bound(0, anglelimit * (1 - (WEP_CVAR_PRI(arc, returnspeed) * dt)), 1);
178                 self.beam_dir = normalize((w_shotdir * (1 - blendfactor)) + (self.beam_dir * blendfactor));
179
180                 // network information: beam direction
181                 self.SendFlags |= 4;
182
183                 // this is where we calculate how many segments are needed
184                 float max_allowed_segments = ARC_MAX_SEGMENTS;
185
186                 #if 0
187                 if(WEP_CVAR_PRI(arc, distancepersegment))
188                         max_allowed_segments = min(ARC_MAX_SEGMENTS, 1 + (distance_to_want_pos / WEP_CVAR_PRI(arc, distancepersegment)));
189                 else
190                         max_allowed_segments = ARC_MAX_SEGMENTS;
191                 #endif
192
193                 if(WEP_CVAR_PRI(arc, degreespersegment))
194                 {
195                         segments = min( max(1, ( min(angle, WEP_CVAR_PRI(arc, maxangle)) / WEP_CVAR_PRI(arc, degreespersegment) ) ), max_allowed_segments );
196                         //segments = min( min(angle, WEP_CVAR_PRI(arc, maxangle)) / WEP_CVAR_PRI(arc, degreespersegment), max_allowed_segments );
197                 }
198                 else { segments = 1; }
199         }
200         else
201         {
202                 segments = 1;
203         }
204
205         vector beam_endpos_estimate = (w_shotorg + (self.beam_dir * WEP_CVAR_PRI(arc, range)));
206
207         #ifdef ARC_DEBUG
208         printf("segment count: %d\n", segments);
209         //string segmentinfo = "";
210         #endif
211
212         float new_beam_type = 0;
213         vector last_origin = w_shotorg;
214         for(i = 1; i <= segments; ++i)
215         {
216                 // this could probably be calculated in a better way
217                 float segmentblend = (i/segments);
218                 float segmentdist = vlen(beam_endpos_estimate - last_origin) * (i/segments);
219
220                 vector new_dir = normalize((w_shotdir * (1 - segmentblend)) + (normalize(beam_endpos_estimate - last_origin) * segmentblend));
221                 vector new_origin = last_origin + (new_dir * segmentdist);
222
223                 WarpZone_traceline_antilag(
224                         self.owner,
225                         last_origin,
226                         new_origin,
227                         MOVE_NORMAL,
228                         self.owner,
229                         ANTILAG_LATENCY(self.owner)
230                 );
231                 
232                 if(trace_ent) // and check if this ent takes damage
233                 {
234                         // calculate our own hit origin as trace_endpos tends to jump around annoyingly (to player origin?)
235                         vector hitorigin = last_origin + (new_dir * segmentdist * trace_fraction);
236
237                         if(SAME_TEAM(self.owner, trace_ent))
238                         {
239                                 // hit a team mate heal them now
240                                 #ifdef ARC_DEBUG
241                                 te_lightning1(self.lg_ents[i - 1], last_origin, hitorigin);
242                                 te_customflash(hitorigin, 80, 5, '1 0 0');
243                                 #endif
244                                 new_beam_type = ARC_BT_HEAL;
245                         }
246                         else
247                         {
248                                 // hit an enemy/other damageable object
249                                 float falloff = ExponentialFalloff(
250                                         WEP_CVAR_PRI(arc, falloff_mindist),
251                                         WEP_CVAR_PRI(arc, falloff_maxdist),
252                                         WEP_CVAR_PRI(arc, falloff_halflifedist),
253                                         vlen(WarpZone_UnTransformOrigin(WarpZone_trace_transform, hitorigin) - w_shotorg)
254                                 );
255
256                                 if(accuracy_isgooddamage(self.owner, trace_ent))
257                                 {
258                                         accuracy_add(
259                                                 self.owner,
260                                                 WEP_ARC,
261                                                 0,
262                                                 WEP_CVAR_PRI(arc, damage) * dt * falloff
263                                         );
264                                 }
265
266                                 Damage(
267                                         trace_ent,
268                                         self.owner,
269                                         self.owner,
270                                         WEP_CVAR_PRI(arc, damage) * dt * falloff,
271                                         WEP_ARC,
272                                         hitorigin,
273                                         WEP_CVAR_PRI(arc, force) * new_dir * dt * falloff
274                                 );
275
276                                 #ifdef ARC_DEBUG
277                                 te_lightning1(self.lg_ents[i - 1], last_origin, hitorigin);
278                                 te_customflash(hitorigin, 80, 5, '1 0 0');
279                                 #endif
280                                 new_beam_type = ARC_BT_ENEMY;
281                         }
282                         break; 
283                 }
284                 else if(trace_fraction != 1)
285                 {
286                         // we collided with geometry
287                         #ifdef ARC_DEBUG
288                         te_lightning1(self.lg_ents[i - 1], last_origin, trace_endpos);
289                         te_customflash(trace_endpos, 50, 2, '0 0 1');
290                         #endif
291                         new_beam_type = ARC_BT_WALL;
292                         break;
293                 }
294                 else
295                 {
296                         #ifdef ARC_DEBUG
297                         te_lightning1(self.lg_ents[i - 1], last_origin, new_origin);
298                         #endif
299                         last_origin = new_origin;
300                 }
301         }
302
303         #ifdef ARC_DEBUG
304         if(!new_beam_type)
305         {
306                 te_customflash(trace_endpos, 50, 2, '0 1 0');
307         }
308         #endif
309
310         // network information: beam type
311         if(new_beam_type != self.beam_type)
312         {
313                 self.SendFlags |= 8;
314                 self.beam_type = new_beam_type;
315         }
316
317         self.owner.lg_fire_prev = time;
318         self.nextthink = time;
319 }
320
321 // Attack functions ========================= 
322 void W_Arc_Beam(void)
323 {
324         print("W_Arc_Beam();\n");
325         // only play fire sound if 0.5 sec has passed since player let go the fire button
326         if(time - self.lg_fire_prev > 0.5)
327                 sound(self, CH_WEAPON_A, "weapons/lgbeam_fire.wav", VOL_BASE, ATTN_NORM);
328
329         entity beam, oldself;
330
331         self.arc_beam = beam = spawn();
332         beam.classname = "W_Arc_Beam";
333         beam.solid = SOLID_NOT;
334         beam.think = W_Arc_Beam_Think;
335         beam.owner = self;
336         beam.movetype = MOVETYPE_NONE;
337         beam.shot_spread = 1;
338         beam.bot_dodge = TRUE;
339         beam.bot_dodgerating = WEP_CVAR_PRI(arc, damage);
340         //Net_LinkEntity(beam, FALSE, 0, W_Arc_Beam_Send);
341
342         oldself = self;
343         self = beam;
344         self.think();
345         self = oldself;
346 }
347
348 float W_Arc(float req)
349 {
350         switch(req)
351         {
352                 case WR_AIM:
353                 {
354                         if(WEP_CVAR_PRI(arc, botaimspeed))
355                                 self.BUTTON_ATCK = bot_aim(WEP_CVAR_PRI(arc, botaimspeed), 0, WEP_CVAR_PRI(arc, botaimlifetime), FALSE);
356                         else
357                                 self.BUTTON_ATCK = bot_aim(1000000, 0, 0.001, FALSE);
358                         return TRUE;
359                 }
360                 case WR_THINK:
361                 {
362                         if(self.BUTTON_ATCK)
363                         {
364                                 if(self.BUTTON_ATCK_prev) // TODO: Find another way to implement this!
365                                         /*if(self.animstate_startframe == self.anim_shoot_x && self.animstate_numframes == self.anim_shoot_y)
366                                                 weapon_thinkf(WFRAME_DONTCHANGE, autocvar_g_balance_arc_primary_animtime, w_ready);
367                                         else*/
368                                                 weapon_thinkf(WFRAME_FIRE1, WEP_CVAR_PRI(arc, animtime), w_ready);
369                                 
370                                 if(weapon_prepareattack(0, 0))
371                                 {
372                                         if((!self.arc_beam) || wasfreed(self.arc_beam))
373                                                 W_Arc_Beam();
374                                         
375                                         if(!self.BUTTON_ATCK_prev)
376                                         {
377                                                 weapon_thinkf(WFRAME_FIRE1, WEP_CVAR_PRI(arc, animtime), w_ready);
378                                                 self.BUTTON_ATCK_prev = 1;
379                                         }
380                                 }
381                         } 
382                         else // todo
383                         {
384                                 if(self.BUTTON_ATCK_prev != 0)
385                                 {
386                                         weapon_thinkf(WFRAME_FIRE1, WEP_CVAR_PRI(arc, animtime), w_ready);
387                                         ATTACK_FINISHED(self) = time + WEP_CVAR_PRI(arc, refire) * W_WeaponRateFactor();
388                                 }
389                                 self.BUTTON_ATCK_prev = 0;
390                         }
391
392                         //if(self.BUTTON_ATCK2)
393                                 //if(weapon_prepareattack(1, autocvar_g_balance_arc_secondary_refire))
394                                 //{
395                                 //      W_Arc_Attack2();
396                                 //      self.arc_count = autocvar_g_balance_arc_secondary_count;
397                                 //      weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_arc_secondary_animtime, w_arc_checkattack);
398                                 //      self.arc_secondarytime = time + autocvar_g_balance_arc_secondary_refire2 * W_WeaponRateFactor();
399                                 //}
400                                 
401                         return TRUE;
402                 }
403                 case WR_INIT:
404                 {
405                         precache_model("models/weapons/g_arc.md3");
406                         precache_model("models/weapons/v_arc.md3");
407                         precache_model("models/weapons/h_arc.iqm");
408                         //precache_sound("weapons/arc_bounce.wav");
409                         precache_sound("weapons/arc_fire.wav");
410                         precache_sound("weapons/arc_fire2.wav");
411                         precache_sound("weapons/arc_impact.wav");
412                         //precache_sound("weapons/arc_impact_combo.wav");
413                         //precache_sound("weapons/W_Arc_Beam_fire.wav");
414                         return TRUE;
415                 }
416                 case WR_CHECKAMMO1:
417                 {
418                         return !WEP_CVAR_PRI(arc, ammo) || (self.WEP_AMMO(ARC) > 0);
419                 }
420                 case WR_CHECKAMMO2:
421                 {
422                         return self.WEP_AMMO(ARC) >= WEP_CVAR_SEC(arc, ammo);
423                 }
424                 case WR_CONFIG:
425                 {
426                         ARC_SETTINGS(WEP_CONFIG_WRITE_CVARS, WEP_CONFIG_WRITE_PROPS)
427                         return TRUE;
428                 }
429                 case WR_KILLMESSAGE:
430                 {
431                         if(w_deathtype & HITTYPE_SECONDARY)
432                         {
433                                 return WEAPON_ELECTRO_MURDER_ORBS;
434                         }
435                         else
436                         {
437                                 if(w_deathtype & HITTYPE_BOUNCE)
438                                         return WEAPON_ELECTRO_MURDER_COMBO;
439                                 else
440                                         return WEAPON_ELECTRO_MURDER_BOLT;
441                         }
442                 }
443                 case WR_RESETPLAYER:
444                 {
445                         //self.arc_secondarytime = time;
446                         return TRUE;
447                 }
448         }
449         return FALSE;
450 }
451
452 void ArcInit(void)
453 {
454         WEP_ACTION(WEP_ARC, WR_INIT);
455         arc_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ARC), FALSE, FALSE, 1);
456         arc_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ARC), FALSE, FALSE, 2);
457         arc_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ARC), FALSE, FALSE, 3);
458         arc_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_ARC), FALSE, FALSE, 4);
459         ARC_SETTINGS(WEP_SKIP_CVAR, WEP_SET_PROP)
460 }
461 #endif
462 #ifdef CSQC
463 float W_Arc(float req)
464 {
465         switch(req)
466         {
467                 case WR_IMPACTEFFECT:
468                 {
469                         vector org2;
470                         org2 = w_org + w_backoff * 6;
471                         
472                         if(w_deathtype & HITTYPE_SECONDARY)
473                         {
474                                 pointparticles(particleeffectnum("arc_ballexplode"), org2, '0 0 0', 1);
475                                 if(!w_issilent)
476                                         sound(self, CH_SHOTS, "weapons/arc_impact.wav", VOL_BASE, ATTN_NORM);
477                         }
478                         else
479                         {
480                                 pointparticles(particleeffectnum("arc_impact"), org2, '0 0 0', 1);
481                                 if(!w_issilent)
482                                         sound(self, CH_SHOTS, "weapons/arc_impact.wav", VOL_BASE, ATTN_NORM);
483                         }
484                         
485                         return TRUE;
486                 }
487                 case WR_INIT:
488                 {
489                         precache_sound("weapons/arc_impact.wav");
490                         precache_sound("weapons/arc_impact_combo.wav");
491                         return TRUE;
492                 }
493                 case WR_ZOOMRETICLE:
494                 {
495                         // no weapon specific image for this weapon
496                         return FALSE;
497                 }
498         }
499         return FALSE;
500 }
501 #endif
502 #endif