Merge branch 'master' into terencehill/music_player
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / w_fireball.qc
1 #ifdef REGISTER_WEAPON
2 REGISTER_WEAPON(
3 /* WEP_##id  */ FIREBALL,
4 /* function  */ W_Fireball,
5 /* ammotype  */ ammo_none,
6 /* impulse   */ 9,
7 /* flags     */ WEP_FLAG_SUPERWEAPON | WEP_TYPE_SPLASH,
8 /* rating    */ BOT_PICKUP_RATING_MID,
9 /* color     */ '1 0.5 0',
10 /* modelname */ "fireball",
11 /* simplemdl */ "foobar",
12 /* crosshair */ "gfx/crosshairfireball",
13 /* wepimg    */ "weaponfireball",
14 /* refname   */ "fireball",
15 /* wepname   */ _("Fireball")
16 );
17
18 #define FIREBALL_SETTINGS(w_cvar,w_prop) FIREBALL_SETTINGS_LIST(w_cvar, w_prop, FIREBALL, fireball)
19 #define FIREBALL_SETTINGS_LIST(w_cvar,w_prop,id,sn) \
20         w_cvar(id, sn, BOTH, animtime) \
21         w_cvar(id, sn, BOTH, refire) \
22         w_cvar(id, sn, BOTH, damage) \
23         w_cvar(id, sn, BOTH, damageforcescale) \
24         w_cvar(id, sn, BOTH, speed) \
25         w_cvar(id, sn, BOTH, spread) \
26         w_cvar(id, sn, BOTH, lifetime) \
27         w_cvar(id, sn, BOTH, laserburntime) \
28         w_cvar(id, sn, BOTH, laserdamage) \
29         w_cvar(id, sn, BOTH, laseredgedamage) \
30         w_cvar(id, sn, BOTH, laserradius) \
31         w_cvar(id, sn, PRI,  edgedamage) \
32         w_cvar(id, sn, PRI,  force) \
33         w_cvar(id, sn, PRI,  radius) \
34         w_cvar(id, sn, PRI,  health) \
35         w_cvar(id, sn, PRI,  refire2) \
36         w_cvar(id, sn, PRI,  bfgdamage) \
37         w_cvar(id, sn, PRI,  bfgforce) \
38         w_cvar(id, sn, PRI,  bfgradius) \
39         w_cvar(id, sn, SEC,  damagetime) \
40         w_cvar(id, sn, SEC,  speed_up) \
41         w_cvar(id, sn, SEC,  speed_z) \
42         w_prop(id, sn, float,  switchdelay_raise, switchdelay_raise) \
43         w_prop(id, sn, float,  switchdelay_drop, switchdelay_drop) \
44         w_prop(id, sn, string, weaponreplace, weaponreplace) \
45         w_prop(id, sn, float,  weaponstart, weaponstart) \
46         w_prop(id, sn, float,  weaponstartoverride, weaponstartoverride) \
47         w_prop(id, sn, float,  weaponthrowable, weaponthrowable)
48
49 #ifdef SVQC
50 FIREBALL_SETTINGS(WEP_ADD_CVAR, WEP_ADD_PROP)
51 .float bot_primary_fireballmooth; // whatever a mooth is
52 .vector fireball_impactvec;
53 .float fireball_primarytime;
54 #endif
55 #else
56 #ifdef SVQC
57 void spawnfunc_weapon_fireball(void) { weapon_defaultspawnfunc(WEP_FIREBALL); }
58
59 void W_Fireball_Explode(void)
60 {
61         entity e;
62         float dist;
63         float points;
64         vector dir;
65         float d;
66
67         self.event_damage = func_null;
68         self.takedamage = DAMAGE_NO;
69
70         // 1. dist damage
71         d = (self.realowner.health + self.realowner.armorvalue);
72         RadiusDamage(self, self.realowner, WEP_CVAR_PRI(fireball, damage), WEP_CVAR_PRI(fireball, edgedamage), WEP_CVAR_PRI(fireball, radius), world, world, WEP_CVAR_PRI(fireball, force), self.projectiledeathtype, other);
73         if(self.realowner.health + self.realowner.armorvalue >= d)
74         if(!self.cnt)
75         {
76                 modeleffect_spawn("models/sphere/sphere.md3", 0, 0, self.origin, '0 0 0', '0 0 0', '0 0 0', 0, WEP_CVAR_PRI(fireball, bfgradius), 0.2, 0.05, 0.25);
77
78                 // 2. bfg effect
79                 // NOTE: this cannot be made warpzone aware by design. So, better intentionally ignore warpzones here.
80                 for(e = findradius(self.origin, WEP_CVAR_PRI(fireball, bfgradius)); e; e = e.chain)
81                 if(e != self.realowner) if(e.takedamage == DAMAGE_AIM) if(!IS_PLAYER(e) || !self.realowner || DIFF_TEAM(e, self))
82                 {
83                         // can we see fireball?
84                         traceline(e.origin + e.view_ofs, self.origin, MOVE_NORMAL, e);
85                         if(/* trace_startsolid || */ trace_fraction != 1) // startsolid should be never happening anyway
86                                 continue;
87                         // can we see player who shot fireball?
88                         traceline(e.origin + e.view_ofs, self.realowner.origin + self.realowner.view_ofs, MOVE_NORMAL, e);
89                         if(trace_ent != self.realowner)
90                         if(/* trace_startsolid || */ trace_fraction != 1)
91                                 continue;
92                         dist = vlen(self.origin - e.origin - e.view_ofs);
93                         points = (1 - sqrt(dist / WEP_CVAR_PRI(fireball, bfgradius)));
94                         if(points <= 0)
95                                 continue;
96                         dir = normalize(e.origin + e.view_ofs - self.origin);
97
98                         if(accuracy_isgooddamage(self.realowner, e))
99                                 accuracy_add(self.realowner, WEP_FIREBALL, 0, WEP_CVAR_PRI(fireball, bfgdamage) * points);
100
101                         Damage(e, self, self.realowner, WEP_CVAR_PRI(fireball, bfgdamage) * points, self.projectiledeathtype | HITTYPE_BOUNCE | HITTYPE_SPLASH, e.origin + e.view_ofs, WEP_CVAR_PRI(fireball, bfgforce) * dir);
102                         pointparticles(particleeffectnum("fireball_bfgdamage"), e.origin, -1 * dir, 1);
103                 }
104         }
105
106         remove(self);
107 }
108
109 void W_Fireball_TouchExplode(void)
110 {
111         PROJECTILE_TOUCH;
112         W_Fireball_Explode();
113 }
114
115 void W_Fireball_LaserPlay(float dt, float dist, float damage, float edgedamage, float burntime)
116 {
117         entity e;
118         float d;
119         vector p;
120
121         if(damage <= 0)
122                 return;
123
124         RandomSelection_Init();
125         for(e = WarpZone_FindRadius(self.origin, dist, TRUE); e; e = e.chain)
126         if(e != self.realowner) if(e.takedamage == DAMAGE_AIM) if(!IS_PLAYER(e) || !self.realowner || DIFF_TEAM(e, self))
127         {
128                 p = e.origin;
129                 p_x += e.mins_x + random() * (e.maxs_x - e.mins_x);
130                 p_y += e.mins_y + random() * (e.maxs_y - e.mins_y);
131                 p_z += e.mins_z + random() * (e.maxs_z - e.mins_z);
132                 d = vlen(WarpZone_UnTransformOrigin(e, self.origin) - p);
133                 if(d < dist)
134                 {
135                         e.fireball_impactvec = p;
136                         RandomSelection_Add(e, 0, string_null, 1 / (1 + d), !Fire_IsBurning(e));
137                 }
138         }
139         if(RandomSelection_chosen_ent)
140         {
141                 d = vlen(WarpZone_UnTransformOrigin(RandomSelection_chosen_ent, self.origin) - RandomSelection_chosen_ent.fireball_impactvec);
142                 d = damage + (edgedamage - damage) * (d / dist);
143                 Fire_AddDamage(RandomSelection_chosen_ent, self.realowner, d * burntime, burntime, self.projectiledeathtype | HITTYPE_BOUNCE);
144                 //trailparticles(self, particleeffectnum("fireball_laser"), self.origin, RandomSelection_chosen_ent.fireball_impactvec);
145                 pointparticles(particleeffectnum("fireball_laser"), self.origin, RandomSelection_chosen_ent.fireball_impactvec - self.origin, 1);
146         }
147 }
148
149 void W_Fireball_Think(void)
150 {
151         if(time > self.pushltime)
152         {
153                 self.cnt = 1;
154                 self.projectiledeathtype |= HITTYPE_SPLASH;
155                 W_Fireball_Explode();
156                 return;
157         }
158
159         W_Fireball_LaserPlay(0.1, WEP_CVAR_PRI(fireball, laserradius), WEP_CVAR_PRI(fireball, laserdamage), WEP_CVAR_PRI(fireball, laseredgedamage), WEP_CVAR_PRI(fireball, laserburntime));
160
161         self.nextthink = time + 0.1;
162 }
163
164 void W_Fireball_Damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
165 {
166         if(self.health <= 0)
167                 return;
168
169         if(!W_CheckProjectileDamage(inflictor.realowner, self.realowner, deathtype, -1)) // no exceptions
170                 return; // g_projectiles_damage says to halt
171
172         self.health = self.health - damage;
173         if(self.health <= 0)
174         {
175                 self.cnt = 1;
176                 W_PrepareExplosionByDamage(attacker, W_Fireball_Explode);
177         }
178 }
179
180 void W_Fireball_Attack1(void)
181 {
182         entity proj;
183
184         W_SetupShot_ProjectileSize(self, '-16 -16 -16', '16 16 16', FALSE, 2, "weapons/fireball_fire2.wav", CH_WEAPON_A, WEP_CVAR_PRI(fireball, damage) + WEP_CVAR_PRI(fireball, bfgdamage));
185
186         pointparticles(particleeffectnum("fireball_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
187
188         proj = spawn();
189         proj.classname = "plasma_prim";
190         proj.owner = proj.realowner = self;
191         proj.bot_dodge = TRUE;
192         proj.bot_dodgerating = WEP_CVAR_PRI(fireball, damage);
193         proj.pushltime = time + WEP_CVAR_PRI(fireball, lifetime);
194         proj.use = W_Fireball_Explode;
195         proj.think = W_Fireball_Think;
196         proj.nextthink = time;
197         proj.health = WEP_CVAR_PRI(fireball, health);
198         proj.team = self.team;
199         proj.event_damage = W_Fireball_Damage;
200         proj.takedamage = DAMAGE_YES;
201         proj.damageforcescale = WEP_CVAR_PRI(fireball, damageforcescale);
202         PROJECTILE_MAKETRIGGER(proj);
203         proj.projectiledeathtype = WEP_FIREBALL;
204         setorigin(proj, w_shotorg);
205
206         proj.movetype = MOVETYPE_FLY;
207         W_SetupProjVelocity_PRI(proj, fireball);
208         proj.angles = vectoangles(proj.velocity);
209         proj.touch = W_Fireball_TouchExplode;
210         setsize(proj, '-16 -16 -16', '16 16 16');
211         proj.flags = FL_PROJECTILE;
212     proj.missile_flags = MIF_SPLASH | MIF_PROXY;
213
214         CSQCProjectile(proj, TRUE, PROJECTILE_FIREBALL, TRUE);
215
216         other = proj; MUTATOR_CALLHOOK(EditProjectile);
217 }
218
219 void W_Fireball_AttackEffect(float i, vector f_diff)
220 {
221         W_SetupShot_ProjectileSize(self, '-16 -16 -16', '16 16 16', FALSE, 0, "", 0, 0);
222         w_shotorg += f_diff_x * v_up + f_diff_y * v_right;
223         pointparticles(particleeffectnum("fireball_preattack_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
224 }
225
226 void W_Fireball_Attack1_Frame4(void)
227 {
228         W_Fireball_Attack1();
229         weapon_thinkf(WFRAME_FIRE1, WEP_CVAR_PRI(fireball, animtime), w_ready);
230 }
231
232 void W_Fireball_Attack1_Frame3(void)
233 {
234         W_Fireball_AttackEffect(0, '+1.25 +3.75 0');
235         weapon_thinkf(WFRAME_FIRE1, WEP_CVAR_PRI(fireball, animtime), W_Fireball_Attack1_Frame4);
236 }
237
238 void W_Fireball_Attack1_Frame2(void)
239 {
240         W_Fireball_AttackEffect(0, '-1.25 +3.75 0');
241         weapon_thinkf(WFRAME_FIRE1, WEP_CVAR_PRI(fireball, animtime), W_Fireball_Attack1_Frame3);
242 }
243
244 void W_Fireball_Attack1_Frame1(void)
245 {
246         W_Fireball_AttackEffect(1, '+1.25 -3.75 0');
247         weapon_thinkf(WFRAME_FIRE1, WEP_CVAR_PRI(fireball, animtime), W_Fireball_Attack1_Frame2);
248 }
249
250 void W_Fireball_Attack1_Frame0(void)
251 {
252         W_Fireball_AttackEffect(0, '-1.25 -3.75 0');
253         sound(self, CH_WEAPON_SINGLE, "weapons/fireball_prefire2.wav", VOL_BASE, ATTEN_NORM);
254         weapon_thinkf(WFRAME_FIRE1, WEP_CVAR_PRI(fireball, animtime), W_Fireball_Attack1_Frame1);
255 }
256
257 void W_Fireball_Firemine_Think(void)
258 {
259         if(time > self.pushltime)
260         {
261                 remove(self);
262                 return;
263         }
264
265         // make it "hot" once it leaves its owner
266         if(self.owner)
267         {
268                 if(vlen(self.origin - self.owner.origin - self.owner.view_ofs) > WEP_CVAR_SEC(fireball, laserradius))
269                 {
270                         self.cnt += 1;
271                         if(self.cnt == 3)
272                                 self.owner = world;
273                 }
274                 else
275                         self.cnt = 0;
276         }
277
278         W_Fireball_LaserPlay(0.1, WEP_CVAR_SEC(fireball, laserradius), WEP_CVAR_SEC(fireball, laserdamage), WEP_CVAR_SEC(fireball, laseredgedamage), WEP_CVAR_SEC(fireball, laserburntime));
279
280         self.nextthink = time + 0.1;
281 }
282
283 void W_Fireball_Firemine_Touch(void)
284 {
285         PROJECTILE_TOUCH;
286         if(other.takedamage == DAMAGE_AIM)
287         if(Fire_AddDamage(other, self.realowner, WEP_CVAR_SEC(fireball, damage), WEP_CVAR_SEC(fireball, damagetime), self.projectiledeathtype) >= 0)
288         {
289                 remove(self);
290                 return;
291         }
292         self.projectiledeathtype |= HITTYPE_BOUNCE;
293 }
294
295 void W_Fireball_Attack2(void)
296 {
297         entity proj;
298         vector f_diff;
299         float c;
300
301         c = mod(self.bulletcounter, 4);
302         switch(c)
303         {
304                 case 0:
305                         f_diff = '-1.25 -3.75 0';
306                         break;
307                 case 1:
308                         f_diff = '+1.25 -3.75 0';
309                         break;
310                 case 2:
311                         f_diff = '-1.25 +3.75 0';
312                         break;
313                 case 3:
314                 default:
315                         f_diff = '+1.25 +3.75 0';
316                         break;
317         }
318         W_SetupShot_ProjectileSize(self, '-4 -4 -4', '4 4 4', FALSE, 2, "weapons/fireball_fire.wav", CH_WEAPON_A, WEP_CVAR_SEC(fireball, damage));
319         traceline(w_shotorg, w_shotorg + f_diff_x * v_up + f_diff_y * v_right, MOVE_NORMAL, self);
320         w_shotorg = trace_endpos;
321
322         pointparticles(particleeffectnum("fireball_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
323
324         proj = spawn();
325         proj.owner = proj.realowner = self;
326         proj.classname = "grenade";
327         proj.bot_dodge = TRUE;
328         proj.bot_dodgerating = WEP_CVAR_SEC(fireball, damage);
329         proj.movetype = MOVETYPE_BOUNCE;
330         proj.projectiledeathtype = WEP_FIREBALL | HITTYPE_SECONDARY;
331         proj.touch = W_Fireball_Firemine_Touch;
332         PROJECTILE_MAKETRIGGER(proj);
333         setsize(proj, '-4 -4 -4', '4 4 4');
334         setorigin(proj, w_shotorg);
335         proj.think = W_Fireball_Firemine_Think;
336         proj.nextthink = time;
337         proj.damageforcescale = WEP_CVAR_SEC(fireball, damageforcescale);
338         proj.pushltime = time + WEP_CVAR_SEC(fireball, lifetime);
339         W_SetupProjVelocity_UP_SEC(proj, fireball);
340
341         proj.angles = vectoangles(proj.velocity);
342         proj.flags = FL_PROJECTILE;
343     proj.missile_flags = MIF_SPLASH | MIF_PROXY | MIF_ARC;
344
345         CSQCProjectile(proj, TRUE, PROJECTILE_FIREMINE, TRUE);
346
347         other = proj; MUTATOR_CALLHOOK(EditProjectile);
348 }
349
350 float W_Fireball(float req)
351 {
352         switch(req)
353         {
354                 case WR_AIM:
355                 {
356                         self.BUTTON_ATCK = FALSE;
357                         self.BUTTON_ATCK2 = FALSE;
358                         if(self.bot_primary_fireballmooth == 0)
359                         {
360                                 if(bot_aim(WEP_CVAR_PRI(fireball, speed), 0, WEP_CVAR_PRI(fireball, lifetime), FALSE))
361                                 {
362                                         self.BUTTON_ATCK = TRUE;
363                                         if(random() < 0.02) self.bot_primary_fireballmooth = 0;
364                                 }
365                         }
366                         else
367                         {
368                                 if(bot_aim(WEP_CVAR_SEC(fireball, speed), WEP_CVAR_SEC(fireball, speed_up), WEP_CVAR_SEC(fireball, lifetime), TRUE))
369                                 {
370                                         self.BUTTON_ATCK2 = TRUE;
371                                         if(random() < 0.01) self.bot_primary_fireballmooth = 1;
372                                 }
373                         }
374                         
375                         return TRUE;
376                 }
377                 case WR_THINK:
378                 {
379                         if(self.BUTTON_ATCK)
380                         {
381                                 if(time >= self.fireball_primarytime)
382                                 if(weapon_prepareattack(0, WEP_CVAR_PRI(fireball, refire)))
383                                 {
384                                         W_Fireball_Attack1_Frame0();
385                                         self.fireball_primarytime = time + WEP_CVAR_PRI(fireball, refire2) * W_WeaponRateFactor();
386                                 }
387                         }
388                         else if(self.BUTTON_ATCK2)
389                         {
390                                 if(weapon_prepareattack(1, WEP_CVAR_SEC(fireball, refire)))
391                                 {
392                                         W_Fireball_Attack2();
393                                         weapon_thinkf(WFRAME_FIRE2, WEP_CVAR_SEC(fireball, animtime), w_ready);
394                                 }
395                         }
396                         
397                         return TRUE;
398                 }
399                 case WR_INIT:
400                 {
401                         precache_model("models/weapons/g_fireball.md3");
402                         precache_model("models/weapons/v_fireball.md3");
403                         precache_model("models/weapons/h_fireball.iqm");
404                         precache_model("models/sphere/sphere.md3");
405                         precache_sound("weapons/fireball_fire.wav");
406                         precache_sound("weapons/fireball_fire2.wav");
407                         precache_sound("weapons/fireball_prefire2.wav");
408                         FIREBALL_SETTINGS(WEP_SKIP_CVAR, WEP_SET_PROP)
409                         return TRUE;
410                 }
411                 case WR_SETUP:
412                 {
413                         self.ammo_field = ammo_none;
414                         return TRUE;
415                 }
416                 case WR_CHECKAMMO1:
417                 case WR_CHECKAMMO2:
418                 {
419                         return TRUE; // fireball has infinite ammo
420                 }
421                 case WR_CONFIG:
422                 {
423                         FIREBALL_SETTINGS(WEP_CONFIG_WRITE_CVARS, WEP_CONFIG_WRITE_PROPS)
424                         return TRUE;
425                 }
426                 case WR_RESETPLAYER:
427                 {
428                         self.fireball_primarytime = time;
429                         return TRUE;
430                 }
431                 case WR_SUICIDEMESSAGE:
432                 {
433                         if(w_deathtype & HITTYPE_SECONDARY)
434                                 return WEAPON_FIREBALL_SUICIDE_FIREMINE;
435                         else
436                                 return WEAPON_FIREBALL_SUICIDE_BLAST;
437                 }
438                 case WR_KILLMESSAGE:
439                 {
440                         if(w_deathtype & HITTYPE_SECONDARY)
441                                 return WEAPON_FIREBALL_MURDER_FIREMINE;
442                         else
443                                 return WEAPON_FIREBALL_MURDER_BLAST;
444                 }
445         }
446         return FALSE;
447 }
448 #endif
449 #ifdef CSQC
450 float W_Fireball(float req)
451 {
452         switch(req)
453         {
454                 case WR_IMPACTEFFECT:
455                 {
456                         vector org2;
457                         if(w_deathtype & HITTYPE_SECONDARY)
458                         {
459                                 // firemine goes out silently
460                         }
461                         else
462                         {
463                                 org2 = w_org + w_backoff * 16;
464                                 pointparticles(particleeffectnum("fireball_explode"), org2, '0 0 0', 1);
465                                 if(!w_issilent)
466                                         sound(self, CH_SHOTS, "weapons/fireball_impact2.wav", VOL_BASE, ATTEN_NORM * 0.25); // long range boom
467                         }
468                         
469                         return TRUE;
470                 }
471                 case WR_INIT:
472                 {
473                         precache_sound("weapons/fireball_impact2.wav");
474                         return TRUE;
475                 }
476                 case WR_ZOOMRETICLE:
477                 {
478                         // no weapon specific image for this weapon
479                         return FALSE;
480                 }
481         }
482         return FALSE;
483 }
484 #endif
485 #endif