]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/weapon/minelayer.qc
Move DMG_NOWEP to defs.qh to reduce compilation unit madness
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / weapon / minelayer.qc
1 #include "minelayer.qh"
2
3 #ifdef SVQC
4
5 void W_MineLayer_Stick(entity this, entity to)
6 {
7         spamsound(this, CH_SHOTS, SND_MINE_STICK, VOL_BASE, ATTN_NORM);
8
9         // in order for mines to face properly when sticking to the ground, they must be a server side entity rather than a csqc projectile
10
11         entity newmine = spawn();
12         IL_PUSH(g_mines, newmine);
13         newmine.weaponentity_fld = this.weaponentity_fld;
14         newmine.classname = this.classname;
15
16         newmine.bot_dodge = this.bot_dodge;
17         newmine.bot_dodgerating = this.bot_dodgerating;
18
19         newmine.owner = this.owner;
20         newmine.realowner = this.realowner;
21         setsize(newmine, '-4 -4 -4', '4 4 4');
22         setorigin(newmine, this.origin);
23         setmodel(newmine, MDL_MINELAYER_MINE);
24         newmine.angles = vectoangles(-trace_plane_normal); // face against the surface
25
26         newmine.mine_orientation = -trace_plane_normal;
27
28         newmine.takedamage = this.takedamage;
29         newmine.damageforcescale = this.damageforcescale;
30         newmine.health = this.health;
31         newmine.event_damage = this.event_damage;
32         newmine.spawnshieldtime = this.spawnshieldtime;
33         newmine.damagedbycontents = true;
34         IL_PUSH(g_damagedbycontents, newmine);
35
36         set_movetype(newmine, MOVETYPE_NONE); // lock the mine in place
37         newmine.projectiledeathtype = this.projectiledeathtype;
38
39         newmine.mine_time = this.mine_time;
40
41         settouch(newmine, func_null);
42         setthink(newmine, W_MineLayer_Think);
43         newmine.nextthink = time;
44         newmine.cnt = this.cnt;
45         newmine.flags = this.flags;
46         IL_PUSH(g_projectiles, newmine);
47         IL_PUSH(g_bot_dodge, newmine);
48
49         delete(this);
50
51         if(to)
52                 SetMovetypeFollow(newmine, to);
53 }
54
55 void W_MineLayer_Explode(entity this, entity directhitentity)
56 {
57         if(directhitentity.takedamage == DAMAGE_AIM)
58                 if(IS_PLAYER(directhitentity))
59                         if(DIFF_TEAM(this.realowner, directhitentity))
60                                 if(!IS_DEAD(directhitentity))
61                                         if(IsFlying(directhitentity))
62                                                 Send_Notification(NOTIF_ONE, this.realowner, MSG_ANNCE, ANNCE_ACHIEVEMENT_AIRSHOT);
63
64         this.event_damage = func_null;
65         this.takedamage = DAMAGE_NO;
66
67         RadiusDamage(this, this.realowner, WEP_CVAR(minelayer, damage), WEP_CVAR(minelayer, edgedamage), WEP_CVAR(minelayer, radius), NULL, NULL, WEP_CVAR(minelayer, force), this.projectiledeathtype, this.weaponentity_fld, directhitentity);
68
69         .entity weaponentity = this.weaponentity_fld;
70         if(this.realowner.(weaponentity).m_weapon == WEP_MINE_LAYER)
71         {
72                 entity own = this.realowner;
73                 Weapon w = WEP_MINE_LAYER;
74                 if(!w.wr_checkammo1(w, own, weaponentity))
75                 {
76                         own.cnt = WEP_MINE_LAYER.m_id;
77                         int slot = weaponslot(weaponentity);
78                         ATTACK_FINISHED(own, slot) = time;
79                         own.(weaponentity).m_switchweapon = w_getbestweapon(own, weaponentity);
80                 }
81         }
82         this.realowner.(weaponentity).minelayer_mines -= 1;
83         delete(this);
84 }
85
86 void W_MineLayer_Explode_think(entity this)
87 {
88         W_MineLayer_Explode(this, NULL);
89 }
90
91 void W_MineLayer_DoRemoteExplode(entity this)
92 {
93         this.event_damage = func_null;
94         this.takedamage = DAMAGE_NO;
95
96         if(this.move_movetype == MOVETYPE_NONE || this.move_movetype == MOVETYPE_FOLLOW)
97                 this.velocity = this.mine_orientation; // particle fx and decals need .velocity
98
99         RadiusDamage(this, this.realowner, WEP_CVAR(minelayer, remote_damage), WEP_CVAR(minelayer, remote_edgedamage), WEP_CVAR(minelayer, remote_radius), 
100                                                 NULL, NULL, WEP_CVAR(minelayer, remote_force), this.projectiledeathtype | HITTYPE_BOUNCE, this.weaponentity_fld, NULL);
101
102         .entity weaponentity = this.weaponentity_fld;
103         if(this.realowner.(weaponentity).m_weapon == WEP_MINE_LAYER)
104         {
105                 entity own = this.realowner;
106                 Weapon w = WEP_MINE_LAYER;
107                 if(!w.wr_checkammo1(w, own, weaponentity))
108                 {
109                         own.cnt = WEP_MINE_LAYER.m_id;
110                         int slot = weaponslot(weaponentity);
111                         ATTACK_FINISHED(own, slot) = time;
112                         own.(weaponentity).m_switchweapon = w_getbestweapon(own, weaponentity);
113                 }
114         }
115         this.realowner.(weaponentity).minelayer_mines -= 1;
116         delete(this);
117 }
118
119 void W_MineLayer_RemoteExplode(entity this)
120 {
121         if(!IS_DEAD(this.realowner))
122                 if((this.spawnshieldtime >= 0)
123                         ? (time >= this.spawnshieldtime) // timer
124                         : (vdist(NearestPointOnBox(this.realowner, this.origin) - this.origin, >, WEP_CVAR(minelayer, remote_radius))) // safety device
125                 )
126                 {
127                         W_MineLayer_DoRemoteExplode(this);
128                 }
129 }
130
131 void W_MineLayer_ProximityExplode(entity this)
132 {
133         // make sure no friend is in the mine's radius. If there is any, explosion is delayed until he's at a safe distance
134         if(WEP_CVAR(minelayer, protection) && this.mine_explodeanyway == 0)
135         {
136                 entity head;
137                 head = findradius(this.origin, WEP_CVAR(minelayer, radius));
138                 while(head)
139                 {
140                         if(head == this.realowner || SAME_TEAM(head, this.realowner))
141                                 return;
142                         head = head.chain;
143                 }
144         }
145
146         this.mine_time = 0;
147         W_MineLayer_Explode(this, NULL);
148 }
149
150 int W_MineLayer_Count(entity e, .entity weaponentity)
151 {
152         int minecount = 0;
153         IL_EACH(g_mines, it.realowner == e && it.weaponentity_fld == weaponentity,
154         {
155                 minecount += 1;
156         });
157
158         return minecount;
159 }
160
161 void W_MineLayer_Think(entity this)
162 {
163         entity head;
164
165         this.nextthink = time;
166
167         if(this.move_movetype == MOVETYPE_FOLLOW)
168         {
169                 if(LostMovetypeFollow(this))
170                 {
171                         UnsetMovetypeFollow(this);
172                         set_movetype(this, MOVETYPE_NONE);
173                 }
174         }
175
176         // our lifetime has expired, it's time to die - mine_time just allows us to play a sound for this
177         // TODO: replace this mine_trigger.wav sound with a real countdown
178         if((time > this.cnt) && (!this.mine_time) && (this.cnt > 0))
179         {
180                 if(WEP_CVAR(minelayer, lifetime_countdown) > 0)
181                         spamsound(this, CH_SHOTS, SND_MINE_TRIGGER, VOL_BASE, ATTN_NORM);
182                 this.mine_time = time + WEP_CVAR(minelayer, lifetime_countdown);
183                 this.mine_explodeanyway = 1; // make the mine super aggressive -- Samual: Rather, make it not care if a team mate is near.
184         }
185
186         // a player's mines shall explode if he disconnects or dies
187         // TODO: Do this on team change too -- Samual: But isn't a player killed when they switch teams?
188         if(!IS_PLAYER(this.realowner) || IS_DEAD(this.realowner) || STAT(FROZEN, this.realowner))
189         {
190                 this.projectiledeathtype |= HITTYPE_BOUNCE;
191                 W_MineLayer_Explode(this, NULL);
192                 return;
193         }
194
195         // set the mine for detonation when a foe gets close enough
196         head = findradius(this.origin, WEP_CVAR(minelayer, proximityradius));
197         while(head)
198         {
199                 if(IS_PLAYER(head) && !IS_DEAD(head) && !STAT(FROZEN, head))
200                 if(head != this.realowner && DIFF_TEAM(head, this.realowner)) // don't trigger for team mates
201                 if(!this.mine_time)
202                 {
203                         spamsound(this, CH_SHOTS, SND_MINE_TRIGGER, VOL_BASE, ATTN_NORM);
204                         this.mine_time = time + WEP_CVAR(minelayer, time);
205                 }
206                 head = head.chain;
207         }
208
209         // explode if it's time to
210         if(this.mine_time && time >= this.mine_time)
211         {
212                 W_MineLayer_ProximityExplode(this);
213                 return;
214         }
215
216         // remote detonation
217         .entity weaponentity = this.weaponentity_fld;
218         if(this.realowner.(weaponentity).m_weapon == WEP_MINE_LAYER)
219         if(!IS_DEAD(this.realowner))
220         if(this.minelayer_detonate)
221                 W_MineLayer_RemoteExplode(this);
222 }
223
224 void W_MineLayer_Touch(entity this, entity toucher)
225 {
226         if(this.move_movetype == MOVETYPE_NONE || this.move_movetype == MOVETYPE_FOLLOW)
227                 return; // we're already a stuck mine, why do we get called? TODO does this even happen?
228
229         if(WarpZone_Projectile_Touch(this, toucher))
230         {
231                 if(wasfreed(this))
232                 {
233                         .entity weaponentity = this.weaponentity_fld;
234                         this.realowner.(weaponentity).minelayer_mines -= 1;
235                 }
236                 return;
237         }
238
239         if((toucher && IS_PLAYER(toucher) && !IS_DEAD(toucher)) || toucher.owner == this.owner)
240         {
241                 // hit a player or other mine
242                 // don't stick
243         }
244         else
245         {
246                 W_MineLayer_Stick(this, toucher);
247         }
248 }
249
250 void W_MineLayer_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
251 {
252         if(this.health <= 0)
253                 return;
254
255         float is_from_enemy = (inflictor.realowner != this.realowner);
256
257         if(!W_CheckProjectileDamage(inflictor.realowner, this.realowner, deathtype, (is_from_enemy ? 1 : -1)))
258                 return; // g_projectiles_damage says to halt
259
260         this.health = this.health - damage;
261         this.angles = vectoangles(this.velocity);
262
263         if(this.health <= 0)
264                 W_PrepareExplosionByDamage(this, attacker, W_MineLayer_Explode_think);
265 }
266
267 void W_MineLayer_Attack(Weapon thiswep, entity actor, .entity weaponentity)
268 {
269         entity mine;
270         entity flash;
271
272         // scan how many mines we placed, and return if we reached our limit
273         if(WEP_CVAR(minelayer, limit))
274         {
275                 if(actor.(weaponentity).minelayer_mines >= WEP_CVAR(minelayer, limit))
276                 {
277                         // the refire delay keeps this message from being spammed
278                         Send_Notification(NOTIF_ONE, actor, MSG_MULTI, WEAPON_MINELAYER_LIMIT, WEP_CVAR(minelayer, limit));
279                         play2(actor, SND(UNAVAILABLE));
280                         return;
281                 }
282         }
283
284         W_DecreaseAmmo(thiswep, actor, WEP_CVAR(minelayer, ammo), weaponentity);
285
286         W_SetupShot_ProjectileSize(actor, weaponentity, '-4 -4 -4', '4 4 4', false, 5, SND_MINE_FIRE, CH_WEAPON_A, WEP_CVAR(minelayer, damage));
287         Send_Effect(EFFECT_ROCKET_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
288
289         mine = WarpZone_RefSys_SpawnSameRefSys(actor);
290         mine.weaponentity_fld = weaponentity;
291         IL_PUSH(g_mines, mine);
292         mine.owner = mine.realowner = actor;
293         if(WEP_CVAR(minelayer, detonatedelay) >= 0)
294                 mine.spawnshieldtime = time + WEP_CVAR(minelayer, detonatedelay);
295         else
296                 mine.spawnshieldtime = -1;
297         mine.classname = "mine";
298         mine.bot_dodge = true;
299         mine.bot_dodgerating = WEP_CVAR(minelayer, damage) * 2; // * 2 because it can detonate inflight which makes it even more dangerous
300
301         mine.takedamage = DAMAGE_YES;
302         mine.damageforcescale = WEP_CVAR(minelayer, damageforcescale);
303         mine.health = WEP_CVAR(minelayer, health);
304         mine.event_damage = W_MineLayer_Damage;
305         mine.damagedbycontents = true;
306         IL_PUSH(g_damagedbycontents, mine);
307
308         set_movetype(mine, MOVETYPE_TOSS);
309         PROJECTILE_MAKETRIGGER(mine);
310         mine.projectiledeathtype = WEP_MINE_LAYER.m_id;
311         mine.weaponentity_fld = weaponentity;
312         setsize(mine, '-4 -4 -4', '4 4 4'); // give it some size so it can be shot
313
314         setorigin(mine, w_shotorg - v_forward * 4); // move it back so it hits the wall at the right point
315         W_SetupProjVelocity_Basic(mine, WEP_CVAR(minelayer, speed), 0);
316         mine.angles = vectoangles(mine.velocity);
317
318         settouch(mine, W_MineLayer_Touch);
319         setthink(mine, W_MineLayer_Think);
320         mine.nextthink = time;
321         mine.cnt = (WEP_CVAR(minelayer, lifetime) - WEP_CVAR(minelayer, lifetime_countdown));
322         mine.flags = FL_PROJECTILE;
323         IL_PUSH(g_projectiles, mine);
324         IL_PUSH(g_bot_dodge, mine);
325         mine.missile_flags = MIF_SPLASH | MIF_ARC | MIF_PROXY;
326
327         if(mine.cnt > 0) { mine.cnt += time; }
328
329         CSQCProjectile(mine, true, PROJECTILE_MINE, true);
330
331         // muzzle flash for 1st person view
332         flash = spawn();
333         setmodel(flash, MDL_MINELAYER_MUZZLEFLASH); // precision set below
334         SUB_SetFade(flash, time, 0.1);
335         flash.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
336         W_AttachToShotorg(actor, weaponentity, flash, '5 0 0');
337
338         // common properties
339
340         MUTATOR_CALLHOOK(EditProjectile, actor, mine);
341
342         actor.(weaponentity).minelayer_mines = W_MineLayer_Count(actor, weaponentity);
343 }
344
345 bool W_MineLayer_PlacedMines(entity this, .entity weaponentity, bool detonate)
346 {
347         bool minfound = false;
348
349         IL_EACH(g_mines, it.realowner == this && it.weaponentity_fld == weaponentity,
350         {
351                 if(detonate)
352                 {
353                         if(!it.minelayer_detonate)
354                         {
355                                 it.minelayer_detonate = true;
356                                 minfound = true;
357                         }
358                 }
359                 else
360                         minfound = true;
361         });
362         return minfound;
363 }
364
365 METHOD(MineLayer, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
366 {
367     // aim and decide to fire if appropriate
368     if(actor.(weaponentity).minelayer_mines >= WEP_CVAR(minelayer, limit))
369         PHYS_INPUT_BUTTON_ATCK(actor) = false;
370     else
371         PHYS_INPUT_BUTTON_ATCK(actor) = bot_aim(actor, weaponentity, WEP_CVAR(minelayer, speed), 0, WEP_CVAR(minelayer, lifetime), false);
372     if(skill >= 2) // skill 0 and 1 bots won't detonate mines!
373     {
374         // decide whether to detonate mines
375         float edgedamage, coredamage, edgeradius, recipricoledgeradius;
376         float selfdamage, teamdamage, enemydamage;
377         edgedamage = WEP_CVAR(minelayer, edgedamage);
378         coredamage = WEP_CVAR(minelayer, damage);
379         edgeradius = WEP_CVAR(minelayer, radius);
380         recipricoledgeradius = 1 / edgeradius;
381         selfdamage = 0;
382         teamdamage = 0;
383         enemydamage = 0;
384
385         IL_EACH(g_mines, it.realowner == actor,
386         {
387                 entity mine = it;
388                 IL_EACH(g_bot_targets, it.bot_attack,
389                 {
390                         float d = vlen(it.origin + (it.mins + it.maxs) * 0.5 - mine.origin);
391                         d = bound(0, edgedamage + (coredamage - edgedamage) * sqrt(1 - d * recipricoledgeradius), 10000);
392                 // count potential damage according to type of target
393                 if(it == actor)
394                     selfdamage = selfdamage + d;
395                 else if(SAME_TEAM(it, actor))
396                     teamdamage = teamdamage + d;
397                 else if(bot_shouldattack(actor, it))
398                     enemydamage = enemydamage + d;
399                 });
400         });
401
402         float desirabledamage;
403         desirabledamage = enemydamage;
404         if(time > actor.invincible_finished && time > actor.spawnshieldtime)
405             desirabledamage = desirabledamage - selfdamage * autocvar_g_balance_selfdamagepercent;
406         if(teamplay && actor.team)
407             desirabledamage = desirabledamage - teamdamage;
408
409         makevectors(actor.v_angle);
410         IL_EACH(g_mines, it.realowner == actor,
411         {
412             if(skill > 9) // normal players only do this for the target they are tracking
413             {
414                     entity mine = it;
415                     IL_EACH(g_bot_targets, it.bot_attack,
416                     {
417                         if((v_forward * normalize(mine.origin - it.origin) < 0.1)
418                             && desirabledamage > 0.1 * coredamage
419                             ) PHYS_INPUT_BUTTON_ATCK2(actor) = true;
420                     });
421                 }
422                 else
423                 {
424                 //As the distance gets larger, a correct detonation gets near imposible
425                 //Bots are assumed to use the mine spawnfunc_light to see if the mine gets near a player
426                 if((v_forward * normalize(it.origin - actor.enemy.origin) < 0.1)
427                         && IS_PLAYER(actor.enemy)
428                         && (desirabledamage >= 0.1 * coredamage)
429                         )
430                 {
431                         float distance = bound(300, vlen(actor.origin - actor.enemy.origin), 30000);
432                         if(random() / distance * 300 > frametime * bound(0, (10 - skill) * 0.2, 1))
433                                 PHYS_INPUT_BUTTON_ATCK2(actor) = true;
434                 }
435                 }
436         });
437
438         // if we would be doing at X percent of the core damage, detonate it
439         // but don't fire a new shot at the same time!
440         if(desirabledamage >= 0.75 * coredamage) //this should do group damage in rare fortunate events
441             PHYS_INPUT_BUTTON_ATCK2(actor) = true;
442         if((skill > 6.5) && (selfdamage > actor.health))
443             PHYS_INPUT_BUTTON_ATCK2(actor) = false;
444         //if(PHYS_INPUT_BUTTON_ATCK2(actor) == true)
445         //      dprint(ftos(desirabledamage),"\n");
446         if(PHYS_INPUT_BUTTON_ATCK2(actor)) PHYS_INPUT_BUTTON_ATCK(actor) = false;
447     }
448 }
449 METHOD(MineLayer, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
450 {
451     if(autocvar_g_balance_minelayer_reload_ammo && actor.(weaponentity).clip_load < WEP_CVAR(minelayer, ammo)) // forced reload
452     {
453         // not if we're holding the minelayer without enough ammo, but can detonate existing mines
454         if(!(W_MineLayer_PlacedMines(actor, weaponentity, false) && GetResourceAmount(actor, thiswep.ammo_type) < WEP_CVAR(minelayer, ammo))) {
455             thiswep.wr_reload(thiswep, actor, weaponentity);
456         }
457     }
458     else if(fire & 1)
459     {
460         if(weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR(minelayer, refire)))
461         {
462             W_MineLayer_Attack(thiswep, actor, weaponentity);
463             weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(minelayer, animtime), w_ready);
464         }
465     }
466
467     if(fire & 2)
468     {
469         if(W_MineLayer_PlacedMines(actor, weaponentity, true))
470             sound(actor, CH_WEAPON_B, SND_MINE_DET, VOL_BASE, ATTN_NORM);
471     }
472 }
473 METHOD(MineLayer, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
474 {
475     //int slot = 0; // TODO: unhardcode
476     // actually do // don't switch while placing a mine
477     //if(ATTACK_FINISHED(actor, slot) <= time || PS(actor).m_weapon != WEP_MINE_LAYER)
478     //{
479         float ammo_amount = GetResourceAmount(actor, thiswep.ammo_type) >= WEP_CVAR(minelayer, ammo);
480         ammo_amount += actor.(weaponentity).(weapon_load[WEP_MINE_LAYER.m_id]) >= WEP_CVAR(minelayer, ammo);
481         return ammo_amount;
482     //}
483     //return true;
484 }
485 METHOD(MineLayer, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
486 {
487     if(W_MineLayer_PlacedMines(actor, weaponentity, false))
488         return true;
489     else
490         return false;
491 }
492 METHOD(MineLayer, wr_resetplayer, void(entity thiswep, entity actor))
493 {
494     for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
495     {
496         .entity weaponentity = weaponentities[slot];
497         actor.(weaponentity).minelayer_mines = 0;
498     }
499 }
500 METHOD(MineLayer, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
501 {
502     W_Reload(actor, weaponentity, WEP_CVAR(minelayer, ammo), SND_RELOAD);
503 }
504 METHOD(MineLayer, wr_suicidemessage, Notification(entity thiswep))
505 {
506     return WEAPON_MINELAYER_SUICIDE;
507 }
508 METHOD(MineLayer, wr_killmessage, Notification(entity thiswep))
509 {
510     return WEAPON_MINELAYER_MURDER;
511 }
512
513 #endif
514 #ifdef CSQC
515
516 METHOD(MineLayer, wr_impacteffect, void(entity thiswep, entity actor))
517 {
518     vector org2;
519     org2 = w_org + w_backoff * 12;
520     pointparticles(EFFECT_ROCKET_EXPLODE, org2, '0 0 0', 1);
521     if(!w_issilent)
522         sound(actor, CH_SHOTS, SND_MINE_EXP, VOL_BASE, ATTN_NORM);
523 }
524
525 #endif