]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/w_shockwave.qc
Merge branch 'master' into terencehill/insta_notifications
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / w_shockwave.qc
1 #ifdef REGISTER_WEAPON
2 REGISTER_WEAPON(
3 /* WEP_##id  */ SHOCKWAVE,
4 /* function  */ W_Shockwave,
5 /* ammotype  */ ammo_none,
6 /* impulse   */ 2,
7 /* flags     */ WEP_FLAG_NORMAL | WEP_TYPE_HITSCAN | WEP_FLAG_CANCLIMB | WEP_FLAG_MUTATORBLOCKED,
8 /* rating    */ BOT_PICKUP_RATING_LOW,
9 /* color     */ '0.5 0.25 0',
10 /* modelname */ "shotgun",
11 /* simplemdl */ "foobar",
12 /* crosshair */ "gfx/crosshairshotgun 0.7",
13 /* wepimg    */ "weaponshotgun",
14 /* refname   */ "shockwave",
15 /* wepname   */ _("Shockwave")
16 );
17
18 #define SHOCKWAVE_SETTINGS(w_cvar,w_prop) SHOCKWAVE_SETTINGS_LIST(w_cvar, w_prop, SHOCKWAVE, shockwave)
19 #define SHOCKWAVE_SETTINGS_LIST(w_cvar,w_prop,id,sn) \
20         w_cvar(id, sn, NONE, blast_animtime) \
21         w_cvar(id, sn, NONE, blast_damage) \
22         w_cvar(id, sn, NONE, blast_distance) \
23         w_cvar(id, sn, NONE, blast_edgedamage) \
24         w_cvar(id, sn, NONE, blast_force) \
25         w_cvar(id, sn, NONE, blast_force_forwardbias) \
26         w_cvar(id, sn, NONE, blast_force_zscale) \
27         w_cvar(id, sn, NONE, blast_jump_damage) \
28         w_cvar(id, sn, NONE, blast_jump_edgedamage) \
29         w_cvar(id, sn, NONE, blast_jump_force) \
30         w_cvar(id, sn, NONE, blast_jump_force_velocitybias) \
31         w_cvar(id, sn, NONE, blast_jump_force_zscale) \
32         w_cvar(id, sn, NONE, blast_jump_multiplier_accuracy) \
33         w_cvar(id, sn, NONE, blast_jump_multiplier_distance) \
34         w_cvar(id, sn, NONE, blast_jump_multiplier_min) \
35         w_cvar(id, sn, NONE, blast_jump_radius) \
36         w_cvar(id, sn, NONE, blast_multiplier_accuracy) \
37         w_cvar(id, sn, NONE, blast_multiplier_distance) \
38         w_cvar(id, sn, NONE, blast_multiplier_min) \
39         w_cvar(id, sn, NONE, blast_refire) \
40         w_cvar(id, sn, NONE, blast_splash_damage) \
41         w_cvar(id, sn, NONE, blast_splash_edgedamage) \
42         w_cvar(id, sn, NONE, blast_splash_force) \
43         w_cvar(id, sn, NONE, blast_splash_force_forwardbias) \
44         w_cvar(id, sn, NONE, blast_splash_multiplier_accuracy) \
45         w_cvar(id, sn, NONE, blast_splash_multiplier_distance) \
46         w_cvar(id, sn, NONE, blast_splash_multiplier_min) \
47         w_cvar(id, sn, NONE, blast_splash_radius) \
48         w_cvar(id, sn, NONE, blast_spread_max) \
49         w_cvar(id, sn, NONE, blast_spread_min) \
50         w_cvar(id, sn, NONE, melee_animtime) \
51         w_cvar(id, sn, NONE, melee_damage) \
52         w_cvar(id, sn, NONE, melee_delay) \
53         w_cvar(id, sn, NONE, melee_force) \
54         w_cvar(id, sn, NONE, melee_multihit) \
55         w_cvar(id, sn, NONE, melee_no_doubleslap) \
56         w_cvar(id, sn, NONE, melee_nonplayerdamage) \
57         w_cvar(id, sn, NONE, melee_range) \
58         w_cvar(id, sn, NONE, melee_refire) \
59         w_cvar(id, sn, NONE, melee_swing_side) \
60         w_cvar(id, sn, NONE, melee_swing_up) \
61         w_cvar(id, sn, NONE, melee_time) \
62         w_cvar(id, sn, NONE, melee_traces) \
63         w_prop(id, sn, float,  switchdelay_raise, switchdelay_raise) \
64         w_prop(id, sn, float,  switchdelay_drop, switchdelay_drop) \
65         w_prop(id, sn, string, weaponreplace, weaponreplace) \
66         w_prop(id, sn, float,  weaponstart, weaponstart) \
67         w_prop(id, sn, float,  weaponstartoverride, weaponstartoverride) \
68         w_prop(id, sn, float,  weaponthrowable, weaponthrowable)
69
70 #ifdef SVQC
71 SHOCKWAVE_SETTINGS(WEP_ADD_CVAR, WEP_ADD_PROP)
72 #endif
73 #ifdef CSQC
74 void Net_ReadShockwaveParticle(void);
75 .vector sw_shotorg;
76 .vector sw_shotdir;
77 .float sw_distance;
78 .float sw_spread_max;
79 .float sw_spread_min;
80 .float sw_time;
81 #endif
82 #else
83 #ifdef SVQC
84 void spawnfunc_weapon_shockwave(void)
85 {
86         //if(autocvar_sv_q3acompat_machineshockwaveswap) // WEAPONTODO
87         if(autocvar_sv_q3acompat_machineshotgunswap)
88         if(self.classname != "droppedweapon")
89         {
90                 weapon_defaultspawnfunc(WEP_MACHINEGUN);
91                 return;
92         }
93         weapon_defaultspawnfunc(WEP_SHOCKWAVE);
94 }
95
96 #define MAX_SHOCKWAVE_HITS 10
97 //#define DEBUG_SHOCKWAVE
98
99 .float swing_prev;
100 .entity swing_alreadyhit;
101 .float shockwave_blasttime;
102 entity shockwave_hit[MAX_SHOCKWAVE_HITS];
103 float shockwave_hit_damage[MAX_SHOCKWAVE_HITS];
104 vector shockwave_hit_force[MAX_SHOCKWAVE_HITS];
105
106 // MELEE ATTACK MODE
107 void W_Shockwave_Melee_Think(void)
108 {
109         // declarations
110         float i, f, swing, swing_factor, swing_damage, meleetime, is_player;
111         entity target_victim;
112         vector targpos;
113
114         // check to see if we can still continue, otherwise give up now
115         if((self.realowner.deadflag != DEAD_NO) && WEP_CVAR(shockwave, melee_no_doubleslap))
116         {
117                 remove(self);
118                 return;
119         }
120
121         // set start time of melee
122         if(!self.cnt)
123         {
124                 self.cnt = time; 
125                 W_PlayStrengthSound(self.realowner);
126         }
127
128         // update values for v_* vectors
129         makevectors(self.realowner.v_angle);
130         
131         // calculate swing percentage based on time
132         meleetime = WEP_CVAR(shockwave, melee_time) * W_WeaponRateFactor();
133         swing = bound(0, (self.cnt + meleetime - time) / meleetime, 10);
134         f = ((1 - swing) * WEP_CVAR(shockwave, melee_traces));
135         
136         // perform the traces needed for this frame 
137         for(i=self.swing_prev; i < f; ++i)
138         {
139                 swing_factor = ((1 - (i / WEP_CVAR(shockwave, melee_traces))) * 2 - 1);
140                 
141                 targpos = (self.realowner.origin + self.realowner.view_ofs 
142                         + (v_forward * WEP_CVAR(shockwave, melee_range))
143                         + (v_up * swing_factor * WEP_CVAR(shockwave, melee_swing_up))
144                         + (v_right * swing_factor * WEP_CVAR(shockwave, melee_swing_side)));
145
146                 WarpZone_traceline_antilag(
147                         self.realowner,
148                         (self.realowner.origin + self.realowner.view_ofs),
149                         targpos,
150                         FALSE,
151                         self.realowner,
152                         ANTILAG_LATENCY(self.realowner)
153                 );
154                 
155                 // draw lightning beams for debugging
156 #ifdef DEBUG_SHOCKWAVE
157                 te_lightning2(world, targpos, self.realowner.origin + self.realowner.view_ofs + v_forward * 5 - v_up * 5); 
158                 te_customflash(targpos, 40,  2, '1 1 1');
159 #endif
160                 
161                 is_player = (IS_PLAYER(trace_ent) || trace_ent.classname == "body" || (trace_ent.flags & FL_MONSTER));
162
163                 if((trace_fraction < 1) // if trace is good, apply the damage and remove self if necessary
164                         && (trace_ent.takedamage == DAMAGE_AIM)  
165                         && (trace_ent != self.swing_alreadyhit)
166                         && (is_player || WEP_CVAR(shockwave, melee_nonplayerdamage)))
167                 {
168                         target_victim = trace_ent; // so it persists through other calls
169                         
170                         if(is_player) // this allows us to be able to nerf the non-player damage done in e.g. assault or onslaught
171                                 swing_damage = (WEP_CVAR(shockwave, melee_damage) * min(1, swing_factor + 1));
172                         else
173                                 swing_damage = (WEP_CVAR(shockwave, melee_nonplayerdamage) * min(1, swing_factor + 1));
174
175                         // trigger damage with this calculated info
176                         Damage(
177                                 target_victim,
178                                 self.realowner,
179                                 self.realowner, 
180                                 swing_damage,
181                                 (WEP_SHOCKWAVE | HITTYPE_SECONDARY), 
182                                 (self.realowner.origin + self.realowner.view_ofs), 
183                                 (v_forward * WEP_CVAR(shockwave, melee_force))
184                         );
185
186                         // handle accuracy
187                         if(accuracy_isgooddamage(self.realowner, target_victim))
188                                 { accuracy_add(self.realowner, WEP_SHOCKWAVE, 0, swing_damage); }
189
190                         #ifdef DEBUG_SHOCKWAVE
191                         print(sprintf(
192                                 "MELEE: %s hitting %s with %f damage (factor: %f) at %f time.\n",
193                                 self.realowner.netname,
194                                 target_victim.netname,
195                                 swing_damage,
196                                 swing_factor,
197                                 time
198                         ));
199                         #endif
200
201                         // allow multiple hits with one swing, but not against the same player twice
202                         if(WEP_CVAR(shockwave, melee_multihit))
203                         {
204                                 self.swing_alreadyhit = target_victim;
205                                 continue; // move along to next trace
206                         }
207                         else
208                         {
209                                 remove(self);
210                                 return;
211                         }
212                 }
213         }
214         
215         if(time >= self.cnt + meleetime)
216         {
217                 // melee is finished
218                 remove(self);
219                 return;
220         }
221         else
222         {
223                 // set up next frame 
224                 self.swing_prev = i;
225                 self.nextthink = time;
226         }
227 }
228
229 void W_Shockwave_Melee(void)
230 {
231         sound(self, CH_WEAPON_A, "weapons/shotgun_melee.wav", VOL_BASE, ATTN_NORM);
232         weapon_thinkf(WFRAME_FIRE2, WEP_CVAR(shockwave, melee_animtime), w_ready);
233
234         entity meleetemp;
235         meleetemp = spawn();
236         meleetemp.owner = meleetemp.realowner = self;
237         meleetemp.think = W_Shockwave_Melee_Think;
238         meleetemp.nextthink = time + WEP_CVAR(shockwave, melee_delay) * W_WeaponRateFactor();
239         W_SetupShot_Range(self, TRUE, 0, "", 0, WEP_CVAR(shockwave, melee_damage), WEP_CVAR(shockwave, melee_range));
240 }
241
242 // SHOCKWAVE ATTACK MODE
243 float W_Shockwave_Attack_CheckSpread(
244         vector targetorg,
245         vector nearest_on_line,
246         vector sw_shotorg,
247         vector attack_endpos)
248 {
249         float spreadlimit;
250         float distance_of_attack = vlen(sw_shotorg - attack_endpos);
251         float distance_from_line = vlen(targetorg - nearest_on_line);
252         
253         spreadlimit = (distance_of_attack ? min(1, (vlen(sw_shotorg - nearest_on_line) / distance_of_attack)) : 1);
254         spreadlimit =
255                 (
256                         (WEP_CVAR(shockwave, blast_spread_min) * (1 - spreadlimit))
257                         +
258                         (WEP_CVAR(shockwave, blast_spread_max) * spreadlimit)
259                 );
260
261         if(
262                 (spreadlimit && (distance_from_line <= spreadlimit))
263                 &&
264                 ((vlen(normalize(targetorg - sw_shotorg) - normalize(attack_endpos - sw_shotorg)) * RAD2DEG) <= 90)
265         )
266                 { return bound(0, (distance_from_line / spreadlimit), 1); }
267         else
268                 { return FALSE; }
269 }
270
271 float W_Shockwave_Attack_IsVisible(
272         entity head,
273         vector nearest_on_line,
274         vector sw_shotorg,
275         vector attack_endpos)
276 {
277         vector nearest_to_attacker = head.WarpZone_findradius_nearest;
278         vector center = (head.origin + (head.mins + head.maxs) * 0.5);
279         vector corner;
280         float i;
281
282         // STEP ONE: Check if the nearest point is clear
283         if(W_Shockwave_Attack_CheckSpread(nearest_to_attacker, nearest_on_line, sw_shotorg, attack_endpos))
284         {
285                 WarpZone_TraceLine(sw_shotorg, nearest_to_attacker, MOVE_NOMONSTERS, self);
286                 if(trace_fraction == 1) { return TRUE; } // yes, the nearest point is clear and we can allow the damage
287         }
288
289         // STEP TWO: Check if shotorg to center point is clear
290         if(W_Shockwave_Attack_CheckSpread(center, nearest_on_line, sw_shotorg, attack_endpos))
291         {
292                 WarpZone_TraceLine(sw_shotorg, center, MOVE_NOMONSTERS, self);
293                 if(trace_fraction == 1) { return TRUE; } // yes, the center point is clear and we can allow the damage
294         }
295
296         // STEP THREE: Check each corner to see if they are clear
297         for(i=1; i<=8; ++i)
298         {
299                 corner = get_corner_position(head, i);
300                 if(W_Shockwave_Attack_CheckSpread(corner, nearest_on_line, sw_shotorg, attack_endpos))
301                 {
302                         WarpZone_TraceLine(sw_shotorg, corner, MOVE_NOMONSTERS, self);
303                         if(trace_fraction == 1) { return TRUE; } // yes, this corner is clear and we can allow the damage
304                 }
305         }
306
307         return FALSE;
308 }
309
310 float W_Shockwave_Attack_CheckHit(
311         float queue,
312         entity head,
313         vector final_force,
314         float final_damage)
315 {
316         if(!head) { return FALSE; }
317         float i;
318         
319         for(i = 0; i <= queue; ++i)
320         {
321                 if(shockwave_hit[i] == head)
322                 {
323                         if(vlen(final_force) > vlen(shockwave_hit_force[i])) { shockwave_hit_force[i] = final_force; }
324                         if(final_damage > shockwave_hit_damage[i]) { shockwave_hit_damage[i] = final_damage; }
325                         return FALSE;
326                 }
327         }
328
329         shockwave_hit[queue] = head;
330         shockwave_hit_force[queue] = final_force;
331         shockwave_hit_damage[queue] = final_damage;
332         return TRUE;
333 }
334
335 void W_Shockwave_Send(void)
336 {
337         WriteByte(MSG_BROADCAST, SVC_TEMPENTITY);
338         WriteByte(MSG_BROADCAST, TE_CSQC_SHOCKWAVEPARTICLE);
339         WriteCoord(MSG_BROADCAST, w_shotorg_x);
340         WriteCoord(MSG_BROADCAST, w_shotorg_y);
341         WriteCoord(MSG_BROADCAST, w_shotorg_z);
342         WriteCoord(MSG_BROADCAST, w_shotdir_x);
343         WriteCoord(MSG_BROADCAST, w_shotdir_y);
344         WriteCoord(MSG_BROADCAST, w_shotdir_z);
345         WriteShort(MSG_BROADCAST, WEP_CVAR(shockwave, blast_distance));
346         WriteByte(MSG_BROADCAST, bound(0, WEP_CVAR(shockwave, blast_spread_max), 255));
347         WriteByte(MSG_BROADCAST, bound(0, WEP_CVAR(shockwave, blast_spread_min), 255));
348         WriteByte(MSG_BROADCAST, num_for_edict(self));
349 }
350
351 void W_Shockwave_Attack(void)
352 {
353         // declarations
354         float multiplier, multiplier_from_accuracy, multiplier_from_distance;
355         float final_damage;
356         vector final_force, center, vel;
357         entity head;
358
359         float i, queue = 0;
360         
361         // set up the shot direction
362         W_SetupShot(self, FALSE, 3, "weapons/lasergun_fire.wav", CH_WEAPON_B, WEP_CVAR(shockwave, blast_damage));
363         vector attack_endpos = (w_shotorg + (w_shotdir * WEP_CVAR(shockwave, blast_distance)));
364         WarpZone_TraceLine(w_shotorg, attack_endpos, MOVE_NOMONSTERS, self);
365         vector attack_hitpos = trace_endpos;
366         float distance_to_end = vlen(w_shotorg - attack_endpos);
367         float distance_to_hit = vlen(w_shotorg - attack_hitpos);
368         //entity transform = WarpZone_trace_transform;
369
370         // do the firing effect now
371         W_Shockwave_Send();
372         Damage_DamageInfo(
373                 attack_hitpos,
374                 WEP_CVAR(shockwave, blast_splash_damage),
375                 WEP_CVAR(shockwave, blast_splash_edgedamage),
376                 WEP_CVAR(shockwave, blast_splash_radius),
377                 w_shotdir * WEP_CVAR(shockwave, blast_splash_force),
378                 WEP_SHOCKWAVE,
379                 0,
380                 self
381         );
382
383         // splash damage/jumping trace
384         head = WarpZone_FindRadius(
385                 attack_hitpos,
386                 max(
387                         WEP_CVAR(shockwave, blast_splash_radius),
388                         WEP_CVAR(shockwave, blast_jump_radius)
389                 ),
390                 FALSE
391         );
392         
393         while(head)
394         {
395                 if(head.takedamage)
396                 {
397                         float distance_to_head = vlen(attack_hitpos - head.WarpZone_findradius_nearest);
398                         
399                         if((head == self) && (distance_to_head <= WEP_CVAR(shockwave, blast_jump_radius)))
400                         {
401                                 // ========================
402                                 //  BLAST JUMP CALCULATION
403                                 // ========================
404                                 
405                                 // calculate importance of distance and accuracy for this attack
406                                 multiplier_from_accuracy = (1 -
407                                         (distance_to_head ?
408                                                 min(1, (distance_to_head / WEP_CVAR(shockwave, blast_jump_radius)))
409                                                 :
410                                                 0
411                                         )
412                                 );
413                                 multiplier_from_distance = (1 -
414                                         (distance_to_hit ?
415                                                 min(1, (distance_to_hit / distance_to_end))
416                                                 :
417                                                 0
418                                         )
419                                 );
420                                 multiplier =
421                                         max(
422                                                 WEP_CVAR(shockwave, blast_jump_multiplier_min),
423                                                 (
424                                                         (multiplier_from_accuracy * WEP_CVAR(shockwave, blast_jump_multiplier_accuracy))
425                                                         +
426                                                         (multiplier_from_distance * WEP_CVAR(shockwave, blast_jump_multiplier_distance))
427                                                 )
428                                         );
429
430                                 // calculate damage from multiplier: 1 = "highest" damage, 0 = "lowest" edgedamage
431                                 final_damage =
432                                         (
433                                                 (WEP_CVAR(shockwave, blast_jump_damage) * multiplier)
434                                                 +
435                                                 (WEP_CVAR(shockwave, blast_jump_edgedamage) * (1 - multiplier))
436                                         );
437
438                                 // figure out the direction of force
439                                 vel = normalize(combine_to_vector(head.velocity_x, head.velocity_y, 0));
440                                 vel *=
441                                         (
442                                                 bound(0, (vlen(vel) / autocvar_sv_maxspeed), 1)
443                                                 *
444                                                 WEP_CVAR(shockwave, blast_jump_force_velocitybias)
445                                         );
446                                 final_force = normalize((CENTER_OR_VIEWOFS(head) - attack_hitpos) + vel);
447
448                                 // now multiply the direction by force units
449                                 final_force *= (WEP_CVAR(shockwave, blast_jump_force) * multiplier);
450                                 final_force_z *= WEP_CVAR(shockwave, blast_jump_force_zscale);
451
452                                 // trigger damage with this calculated info
453                                 Damage(
454                                         head,
455                                         self,
456                                         self,
457                                         final_damage,
458                                         WEP_SHOCKWAVE,
459                                         head.origin,
460                                         final_force
461                                 );
462
463                                 #ifdef DEBUG_SHOCKWAVE
464                                 print(sprintf(
465                                         "SELF HIT: multiplier = %f, damage = %f, force = %f... "
466                                         "multiplier_from_accuracy = %f, multiplier_from_distance = %f.\n",
467                                         multiplier,
468                                         final_damage,
469                                         vlen(final_force),
470                                         multiplier_from_accuracy,
471                                         multiplier_from_distance
472                                 ));
473                                 #endif
474                         }
475                         else if(distance_to_head <= WEP_CVAR(shockwave, blast_splash_radius))
476                         {
477                                 // ==========================
478                                 //  BLAST SPLASH CALCULATION
479                                 // ==========================
480                                 
481                                 // calculate importance of distance and accuracy for this attack
482                                 multiplier_from_accuracy = (1 -
483                                         (distance_to_head ?
484                                                 min(1, (distance_to_head / WEP_CVAR(shockwave, blast_splash_radius)))
485                                                 :
486                                                 0
487                                         )
488                                 );
489                                 multiplier_from_distance = (1 -
490                                         (distance_to_hit ?
491                                                 min(1, (distance_to_hit / distance_to_end))
492                                                 :
493                                                 0
494                                         )
495                                 );
496                                 multiplier =
497                                         max(
498                                                 WEP_CVAR(shockwave, blast_splash_multiplier_min),
499                                                 (
500                                                         (multiplier_from_accuracy * WEP_CVAR(shockwave, blast_splash_multiplier_accuracy))
501                                                         +
502                                                         (multiplier_from_distance * WEP_CVAR(shockwave, blast_splash_multiplier_distance))
503                                                 )
504                                         );
505
506                                 // calculate damage from multiplier: 1 = "highest" damage, 0 = "lowest" edgedamage
507                                 final_damage =
508                                         (
509                                                 (WEP_CVAR(shockwave, blast_splash_damage) * multiplier)
510                                                 +
511                                                 (WEP_CVAR(shockwave, blast_splash_edgedamage) * (1 - multiplier))
512                                         );
513
514                                 // figure out the direction of force
515                                 final_force = (w_shotdir * WEP_CVAR(shockwave, blast_splash_force_forwardbias));
516                                 final_force = normalize(CENTER_OR_VIEWOFS(head) - (attack_hitpos - final_force));
517                                 //te_lightning2(world, attack_hitpos, (attack_hitpos + (final_force * 200)));
518
519                                 // now multiply the direction by force units
520                                 final_force *= (WEP_CVAR(shockwave, blast_splash_force) * multiplier);
521                                 final_force_z *= WEP_CVAR(shockwave, blast_force_zscale);
522
523                                 // queue damage with this calculated info
524                                 if(W_Shockwave_Attack_CheckHit(queue, head, final_force, final_damage)) { queue = min(queue + 1, MAX_SHOCKWAVE_HITS); }
525
526                                 #ifdef DEBUG_SHOCKWAVE
527                                 print(sprintf(
528                                         "SPLASH HIT: multiplier = %f, damage = %f, force = %f... "
529                                         "multiplier_from_accuracy = %f, multiplier_from_distance = %f.\n",
530                                         multiplier,
531                                         final_damage,
532                                         vlen(final_force),
533                                         multiplier_from_accuracy,
534                                         multiplier_from_distance
535                                 ));
536                                 #endif
537                         }
538                 }
539                 head = head.chain;
540         }
541
542         // cone damage trace
543         head = WarpZone_FindRadius(w_shotorg, WEP_CVAR(shockwave, blast_distance), FALSE);
544         while(head)
545         {
546                 if((head != self) && head.takedamage)
547                 {
548                         // ========================
549                         //  BLAST CONE CALCULATION
550                         // ========================
551
552                         // if it's a player, use the view origin as reference (stolen from RadiusDamage functions in g_damage.qc) 
553                         center = CENTER_OR_VIEWOFS(head);
554
555                         // find the closest point on the enemy to the center of the attack
556                         float ang; // angle between shotdir and h
557                         float h; // hypotenuse, which is the distance between attacker to head
558                         float a; // adjacent side, which is the distance between attacker and the point on w_shotdir that is closest to head.origin
559                         
560                         h = vlen(center - self.origin);
561                         ang = acos(dotproduct(normalize(center - self.origin), w_shotdir));
562                         a = h * cos(ang);
563                         // WEAPONTODO: replace with simpler method
564
565                         vector nearest_on_line = (w_shotorg + a * w_shotdir);
566                         vector nearest_to_attacker = WarpZoneLib_NearestPointOnBox(center + head.mins, center + head.maxs, nearest_on_line);
567
568                         if((vlen(head.WarpZone_findradius_dist) <= WEP_CVAR(shockwave, blast_distance)) 
569                                 && (W_Shockwave_Attack_IsVisible(head, nearest_on_line, w_shotorg, attack_endpos)))
570                         {
571                                 // calculate importance of distance and accuracy for this attack
572                                 multiplier_from_accuracy = (1 -
573                                         W_Shockwave_Attack_CheckSpread(
574                                                 nearest_to_attacker,
575                                                 nearest_on_line,
576                                                 w_shotorg,
577                                                 attack_endpos
578                                         )
579                                 );
580                                 multiplier_from_distance = (1 -
581                                         (distance_to_hit ?
582                                                 min(1, (vlen(head.WarpZone_findradius_dist) / distance_to_end))
583                                                 :
584                                                 0
585                                         )
586                                 );
587                                 multiplier =
588                                         max(
589                                                 WEP_CVAR(shockwave, blast_multiplier_min),
590                                                 (
591                                                         (multiplier_from_accuracy * WEP_CVAR(shockwave, blast_multiplier_accuracy))
592                                                         +
593                                                         (multiplier_from_distance * WEP_CVAR(shockwave, blast_multiplier_distance))
594                                                 )
595                                         );
596
597                                 // calculate damage from multiplier: 1 = "highest" damage, 0 = "lowest" edgedamage
598                                 final_damage =
599                                         (
600                                                 (WEP_CVAR(shockwave, blast_damage) * multiplier)
601                                                 +
602                                                 (WEP_CVAR(shockwave, blast_edgedamage) * (1 - multiplier))
603                                         );
604
605                                 // figure out the direction of force
606                                 final_force = (w_shotdir * WEP_CVAR(shockwave, blast_force_forwardbias));
607                                 final_force = normalize(center - (nearest_on_line - final_force));
608                                 //te_lightning2(world, nearest_on_line, (attack_hitpos + (final_force * 200)));
609
610                                 // now multiply the direction by force units
611                                 final_force *= (WEP_CVAR(shockwave, blast_force) * multiplier);
612                                 final_force_z *= WEP_CVAR(shockwave, blast_force_zscale);
613
614                                 // queue damage with this calculated info
615                                 if(W_Shockwave_Attack_CheckHit(queue, head, final_force, final_damage)) { queue = min(queue + 1, MAX_SHOCKWAVE_HITS); }
616
617                                 #ifdef DEBUG_SHOCKWAVE
618                                 print(sprintf(
619                                         "BLAST HIT: multiplier = %f, damage = %f, force = %f... "
620                                         "multiplier_from_accuracy = %f, multiplier_from_distance = %f.\n",
621                                         multiplier,
622                                         final_damage,
623                                         vlen(final_force),
624                                         multiplier_from_accuracy,
625                                         multiplier_from_distance
626                                 ));
627                                 #endif
628                         }
629                 }
630                 head = head.chain;
631         }
632
633         for(i = 1; i <= queue; ++i)
634         {
635                 head = shockwave_hit[i-1];
636                 final_force = shockwave_hit_force[i-1];
637                 final_damage = shockwave_hit_damage[i-1];
638                 
639                 Damage(
640                         head,
641                         self,
642                         self,
643                         final_damage,
644                         WEP_SHOCKWAVE,
645                         head.origin,
646                         final_force
647                 );
648
649                 if(accuracy_isgooddamage(self.realowner, head))
650                 {
651                         print("wtf\n");
652                         accuracy_add(self.realowner, WEP_SHOCKWAVE, 0, final_damage);
653                 }
654
655                 #ifdef DEBUG_SHOCKWAVE
656                 print(sprintf(
657                         "SHOCKWAVE by %s: damage = %f, force = %f.\n",
658                         self.netname,
659                         final_damage,
660                         vlen(final_force)
661                 ));
662                 #endif
663                 
664                 shockwave_hit[i-1] = world;
665                 shockwave_hit_force[i-1] = '0 0 0';
666                 shockwave_hit_damage[i-1] = 0;
667         }
668 }
669
670 float W_Shockwave(float req)
671 {
672         switch(req)
673         {
674                 case WR_AIM:
675                 {
676                         if(vlen(self.origin - self.enemy.origin) <= WEP_CVAR(shockwave, melee_range))
677                                 { self.BUTTON_ATCK2 = bot_aim(1000000, 0, 0.001, FALSE); }
678                         else
679                                 { self.BUTTON_ATCK = bot_aim(1000000, 0, 0.001, FALSE); }
680                         
681                         return TRUE;
682                 }
683                 case WR_THINK:
684                 {
685                         if(self.BUTTON_ATCK)
686                         {
687                                 if(time >= self.shockwave_blasttime) // handle refire separately so the secondary can be fired straight after a primary
688                                 {
689                                         if(weapon_prepareattack(0, WEP_CVAR(shockwave, blast_animtime)))
690                                         {
691                                                 W_Shockwave_Attack();
692                                                 self.shockwave_blasttime = time + WEP_CVAR(shockwave, blast_refire) * W_WeaponRateFactor();
693                                                 weapon_thinkf(WFRAME_FIRE1, WEP_CVAR(shockwave, blast_animtime), w_ready);
694                                         }
695                                 }
696                         }
697                         else if(self.BUTTON_ATCK2)
698                         {
699                                 //if(self.clip_load >= 0) // we are not currently reloading
700                                 if(!self.crouch) // no crouchmelee please
701                                 if(weapon_prepareattack(1, WEP_CVAR(shockwave, melee_refire)))
702                                 {
703                                         // attempt forcing playback of the anim by switching to another anim (that we never play) here...
704                                         weapon_thinkf(WFRAME_FIRE1, 0, W_Shockwave_Melee);
705                                 }
706                         }
707                         
708                         return TRUE;
709                 }
710                 case WR_INIT:
711                 {
712                         precache_model("models/uziflash.md3");
713                         precache_model("models/weapons/g_shotgun.md3");
714                         precache_model("models/weapons/v_shotgun.md3");
715                         precache_model("models/weapons/h_shotgun.iqm");
716                         precache_sound("misc/itempickup.wav");
717                         precache_sound("weapons/lasergun_fire.wav");
718                         precache_sound("weapons/shotgun_melee.wav");
719                         SHOCKWAVE_SETTINGS(WEP_SKIP_CVAR, WEP_SET_PROP)
720                         return TRUE;
721                 }
722                 case WR_CHECKAMMO1:
723                 case WR_CHECKAMMO2:
724                 {
725                         // shockwave has infinite ammo
726                         return TRUE;
727                 }
728                 case WR_CONFIG:
729                 {
730                         SHOCKWAVE_SETTINGS(WEP_CONFIG_WRITE_CVARS, WEP_CONFIG_WRITE_PROPS)
731                         return TRUE;
732                 }
733                 case WR_SUICIDEMESSAGE:
734                 {
735                         return WEAPON_THINKING_WITH_PORTALS;
736                 }
737                 case WR_KILLMESSAGE:
738                 {
739                         if(w_deathtype & HITTYPE_SECONDARY)
740                                 return WEAPON_SHOCKWAVE_MURDER_SLAP;
741                         else
742                                 return WEAPON_SHOCKWAVE_MURDER;
743                 }
744         }
745         return FALSE;
746 }
747 #endif
748 #ifdef CSQC
749 // WEAPONTODO: add client side settings for these
750 #define SW_MAXALPHA 0.5
751 #define SW_FADETIME 0.4
752 #define SW_DISTTOMIN 200
753 void Draw_Shockwave()
754 {
755         // fading/removal control
756         float a = bound(0, (SW_MAXALPHA - ((time - self.sw_time) / SW_FADETIME)), SW_MAXALPHA);
757         if(a < ALPHA_MIN_VISIBLE) { remove(self); }
758
759         // WEAPONTODO: save this only once when creating the entity
760         vector sw_color = getcsqcplayercolor(self.sv_entnum); // GetTeamRGB(GetPlayerColor(self.sv_entnum));
761
762         // WEAPONTODO: trace to find what we actually hit
763         vector endpos = (self.sw_shotorg + (self.sw_shotdir * self.sw_distance));
764
765         vectorvectors(self.sw_shotdir);
766         vector right = v_right; // save this for when we do makevectors later
767         vector up = v_up; // save this for when we do makevectors later
768
769         // WEAPONTODO: combine and simplify these calculations
770         vector min_end = ((self.sw_shotorg + (self.sw_shotdir * SW_DISTTOMIN)) + (up * self.sw_spread_min));
771         vector max_end = (endpos + (up * self.sw_spread_max));
772         float spread_to_min = vlen(normalize(min_end - self.sw_shotorg) - self.sw_shotdir);
773         float spread_to_max = vlen(normalize(max_end - min_end) - self.sw_shotdir);
774
775         vector first_min_end = '0 0 0', prev_min_end = '0 0 0', new_min_end = '0 0 0';
776         vector first_max_end = '0 0 0', prev_max_end = '0 0 0', new_max_end = '0 0 0';
777         float new_max_dist, new_min_dist;
778
779         vector deviation, angle = '0 0 0';
780         float counter, divisions = 20;
781         for(counter = 0; counter < divisions; ++counter)
782         {
783                 // perfect circle effect lines
784                 makevectors('0 360 0' * (0.75 + (counter - 0.5) / divisions));
785                 angle_y = v_forward_x;
786                 angle_z = v_forward_y;
787
788                 // first do the spread_to_min effect
789                 deviation = angle * spread_to_min;
790                 deviation = ((self.sw_shotdir + (right * deviation_y) + (up * deviation_z)));
791                 new_min_dist = SW_DISTTOMIN;
792                 new_min_end = (self.sw_shotorg + (deviation * new_min_dist));
793                 //te_lightning2(world, new_min_end, self.sw_shotorg);
794
795                 // then calculate spread_to_max effect
796                 deviation = angle * spread_to_max;
797                 deviation = ((self.sw_shotdir + (right * deviation_y) + (up * deviation_z)));
798                 new_max_dist = vlen(new_min_end - endpos);
799                 new_max_end = (new_min_end + (deviation * new_max_dist));
800                 //te_lightning2(world, new_end, prev_min_end);
801                 
802
803                 if(counter == 0)
804                 {
805                         first_min_end = new_min_end;
806                         first_max_end = new_max_end;
807                 }
808
809                 if(counter >= 1)
810                 {
811                         // draw from shot origin to min spread radius
812                         R_BeginPolygon("", DRAWFLAG_NORMAL);
813                         R_PolygonVertex(prev_min_end, '0 0 0', sw_color, a);
814                         R_PolygonVertex(new_min_end, '0 0 0', sw_color, a);
815                         R_PolygonVertex(self.sw_shotorg, '0 0 0', sw_color, a);
816                         R_EndPolygon();
817
818                         // draw from min spread radius to max spread radius
819                         R_BeginPolygon("", DRAWFLAG_NORMAL);
820                         R_PolygonVertex(new_min_end, '0 0 0', sw_color, a);
821                         R_PolygonVertex(prev_min_end, '0 0 0', sw_color, a);
822                         R_PolygonVertex(prev_max_end, '0 0 0', sw_color, a);
823                         R_PolygonVertex(new_max_end, '0 0 0', sw_color, a);
824                         R_EndPolygon();
825                 }
826
827                 prev_min_end = new_min_end;
828                 prev_max_end = new_max_end;
829
830                 // last division only
831                 if((counter + 1) == divisions)
832                 {
833                         // draw from shot origin to min spread radius
834                         R_BeginPolygon("", DRAWFLAG_NORMAL);
835                         R_PolygonVertex(prev_min_end, '0 0 0', sw_color, a);
836                         R_PolygonVertex(first_min_end, '0 0 0', sw_color, a);
837                         R_PolygonVertex(self.sw_shotorg, '0 0 0', sw_color, a);
838                         R_EndPolygon();
839
840                         // draw from min spread radius to max spread radius
841                         R_BeginPolygon("", DRAWFLAG_NORMAL);
842                         R_PolygonVertex(first_min_end, '0 0 0', sw_color, a);
843                         R_PolygonVertex(prev_min_end, '0 0 0', sw_color, a);
844                         R_PolygonVertex(prev_max_end, '0 0 0', sw_color, a);
845                         R_PolygonVertex(first_max_end, '0 0 0', sw_color, a);
846                         R_EndPolygon();
847                 }
848         }
849 }
850
851 void Net_ReadShockwaveParticle(void)
852 {
853         entity shockwave;
854         shockwave = spawn();
855         shockwave.draw = Draw_Shockwave;
856         
857         shockwave.sw_shotorg_x = ReadCoord(); shockwave.sw_shotorg_y = ReadCoord(); shockwave.sw_shotorg_z = ReadCoord();
858         shockwave.sw_shotdir_x  = ReadCoord(); shockwave.sw_shotdir_y  = ReadCoord(); shockwave.sw_shotdir_z  = ReadCoord();
859
860         shockwave.sw_distance = ReadShort();
861         shockwave.sw_spread_max = ReadByte();
862         shockwave.sw_spread_min = ReadByte();
863
864         shockwave.sv_entnum = ReadByte();
865
866         shockwave.sw_time = time;
867 }
868
869 float W_Shockwave(float req)
870 {
871         switch(req)
872         {
873                 case WR_IMPACTEFFECT:
874                 {
875                         // handled by Net_ReadShockwaveParticle
876                         //vector org2;
877                         //org2 = w_org + w_backoff * 2;
878                         //pointparticles(particleeffectnum("laser_impact"), org2, w_backoff * 1000, 1);
879                         return FALSE;
880                 }
881                 case WR_INIT:
882                 {
883                         //precache_sound("weapons/ric1.wav");
884                         //precache_sound("weapons/ric2.wav");
885                         //precache_sound("weapons/ric3.wav");
886                         return FALSE;
887                 }
888                 case WR_ZOOMRETICLE:
889                 {
890                         // no weapon specific image for this weapon
891                         return FALSE;
892                 }
893         }
894         return FALSE;
895 }
896 #endif
897 #endif