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