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