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