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