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