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