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