]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/weapon/minelayer.qc
Merge branch 'terencehill/bot_waypoints' into terencehill/bot_ai
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / weapon / minelayer.qc
1 #include "minelayer.qh"
2
3 #ifdef SVQC
4 spawnfunc(weapon_minelayer) { weapon_defaultspawnfunc(this, WEP_MINE_LAYER); }
5
6 void W_MineLayer_Stick(entity this, entity to)
7 {
8         spamsound(this, CH_SHOTS, SND_MINE_STICK, VOL_BASE, ATTN_NORM);
9
10         // in order for mines to face properly when sticking to the ground, they must be a server side entity rather than a csqc projectile
11
12         entity newmine = spawn();
13         IL_PUSH(g_mines, newmine);
14         newmine.weaponentity_fld = this.weaponentity_fld;
15         newmine.classname = this.classname;
16
17         newmine.bot_dodge = this.bot_dodge;
18         newmine.bot_dodgerating = this.bot_dodgerating;
19
20         newmine.owner = this.owner;
21         newmine.realowner = this.realowner;
22         setsize(newmine, '-4 -4 -4', '4 4 4');
23         setorigin(newmine, this.origin);
24         setmodel(newmine, MDL_MINELAYER_MINE);
25         newmine.angles = vectoangles(-trace_plane_normal); // face against the surface
26
27         newmine.mine_orientation = -trace_plane_normal;
28
29         newmine.takedamage = this.takedamage;
30         newmine.damageforcescale = this.damageforcescale;
31         newmine.health = this.health;
32         newmine.event_damage = this.event_damage;
33         newmine.spawnshieldtime = this.spawnshieldtime;
34         newmine.damagedbycontents = true;
35         IL_PUSH(g_damagedbycontents, newmine);
36
37         set_movetype(newmine, MOVETYPE_NONE); // lock the mine in place
38         newmine.projectiledeathtype = this.projectiledeathtype;
39
40         newmine.mine_time = this.mine_time;
41
42         settouch(newmine, func_null);
43         setthink(newmine, W_MineLayer_Think);
44         newmine.nextthink = time;
45         newmine.cnt = this.cnt;
46         newmine.flags = this.flags;
47         IL_PUSH(g_projectiles, newmine);
48         IL_PUSH(g_bot_dodge, newmine);
49
50         delete(this);
51
52         if(to)
53                 SetMovetypeFollow(newmine, to);
54 }
55
56 void W_MineLayer_Explode(entity this, entity directhitentity)
57 {
58         if(directhitentity.takedamage == DAMAGE_AIM)
59                 if(IS_PLAYER(directhitentity))
60                         if(DIFF_TEAM(this.realowner, directhitentity))
61                                 if(!IS_DEAD(directhitentity))
62                                         if(IsFlying(directhitentity))
63                                                 Send_Notification(NOTIF_ONE, this.realowner, MSG_ANNCE, ANNCE_ACHIEVEMENT_AIRSHOT);
64
65         this.event_damage = func_null;
66         this.takedamage = DAMAGE_NO;
67
68         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);
69
70         .entity weaponentity = this.weaponentity_fld;
71         if(this.realowner.(weaponentity).m_weapon == WEP_MINE_LAYER)
72         {
73                 entity own = this.realowner;
74                 Weapon w = WEP_MINE_LAYER;
75                 if(!w.wr_checkammo1(w, own, weaponentity))
76                 {
77                         own.cnt = WEP_MINE_LAYER.m_id;
78                         int slot = weaponslot(weaponentity);
79                         ATTACK_FINISHED(own, slot) = time;
80                         own.(weaponentity).m_switchweapon = w_getbestweapon(own, weaponentity);
81                 }
82         }
83         this.realowner.(weaponentity).minelayer_mines -= 1;
84         delete(this);
85 }
86
87 void W_MineLayer_Explode_think(entity this)
88 {
89         W_MineLayer_Explode(this, NULL);
90 }
91
92 void W_MineLayer_DoRemoteExplode(entity this)
93 {
94         this.event_damage = func_null;
95         this.takedamage = DAMAGE_NO;
96
97         if(this.move_movetype == MOVETYPE_NONE || this.move_movetype == MOVETYPE_FOLLOW)
98                 this.velocity = this.mine_orientation; // particle fx and decals need .velocity
99
100         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);
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, 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         setsize(mine, '-4 -4 -4', '4 4 4'); // give it some size so it can be shot
312
313         setorigin(mine, w_shotorg - v_forward * 4); // move it back so it hits the wall at the right point
314         W_SetupProjVelocity_Basic(mine, WEP_CVAR(minelayer, speed), 0);
315         mine.angles = vectoangles(mine.velocity);
316
317         settouch(mine, W_MineLayer_Touch);
318         setthink(mine, W_MineLayer_Think);
319         mine.nextthink = time;
320         mine.cnt = (WEP_CVAR(minelayer, lifetime) - WEP_CVAR(minelayer, lifetime_countdown));
321         mine.flags = FL_PROJECTILE;
322         IL_PUSH(g_projectiles, mine);
323         IL_PUSH(g_bot_dodge, mine);
324         mine.missile_flags = MIF_SPLASH | MIF_ARC | MIF_PROXY;
325
326         if(mine.cnt > 0) { mine.cnt += time; }
327
328         CSQCProjectile(mine, true, PROJECTILE_MINE, true);
329
330         // muzzle flash for 1st person view
331         flash = spawn();
332         setmodel(flash, MDL_MINELAYER_MUZZLEFLASH); // precision set below
333         SUB_SetFade(flash, time, 0.1);
334         flash.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
335         W_AttachToShotorg(actor, weaponentity, flash, '5 0 0');
336
337         // common properties
338
339         MUTATOR_CALLHOOK(EditProjectile, actor, mine);
340
341         actor.(weaponentity).minelayer_mines = W_MineLayer_Count(actor, weaponentity);
342 }
343
344 bool W_MineLayer_PlacedMines(entity this, .entity weaponentity, bool detonate)
345 {
346         bool minfound = false;
347
348         IL_EACH(g_mines, it.realowner == this && it.weaponentity_fld == weaponentity,
349         {
350                 if(detonate)
351                 {
352                         if(!it.minelayer_detonate)
353                         {
354                                 it.minelayer_detonate = true;
355                                 minfound = true;
356                         }
357                 }
358                 else
359                         minfound = true;
360         });
361         return minfound;
362 }
363
364 METHOD(MineLayer, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
365 {
366     // aim and decide to fire if appropriate
367     if(actor.(weaponentity).minelayer_mines >= WEP_CVAR(minelayer, limit))
368         PHYS_INPUT_BUTTON_ATCK(actor) = false;
369     else
370         PHYS_INPUT_BUTTON_ATCK(actor) = bot_aim(actor, weaponentity, WEP_CVAR(minelayer, speed), 0, WEP_CVAR(minelayer, lifetime), false);
371     if(skill >= 2) // skill 0 and 1 bots won't detonate mines!
372     {
373         // decide whether to detonate mines
374         float edgedamage, coredamage, edgeradius, recipricoledgeradius;
375         float selfdamage, teamdamage, enemydamage;
376         edgedamage = WEP_CVAR(minelayer, edgedamage);
377         coredamage = WEP_CVAR(minelayer, damage);
378         edgeradius = WEP_CVAR(minelayer, radius);
379         recipricoledgeradius = 1 / edgeradius;
380         selfdamage = 0;
381         teamdamage = 0;
382         enemydamage = 0;
383
384         IL_EACH(g_mines, it.realowner == actor,
385         {
386                 entity mine = it;
387                 IL_EACH(g_bot_targets, it.bot_attack,
388                 {
389                         float d = vlen(it.origin + (it.mins + it.maxs) * 0.5 - mine.origin);
390                         d = bound(0, edgedamage + (coredamage - edgedamage) * sqrt(1 - d * recipricoledgeradius), 10000);
391                 // count potential damage according to type of target
392                 if(it == actor)
393                     selfdamage = selfdamage + d;
394                 else if(SAME_TEAM(it, actor))
395                     teamdamage = teamdamage + d;
396                 else if(bot_shouldattack(actor, it))
397                     enemydamage = enemydamage + d;
398                 });
399         });
400
401         float desirabledamage;
402         desirabledamage = enemydamage;
403         if(time > actor.invincible_finished && time > actor.spawnshieldtime)
404             desirabledamage = desirabledamage - selfdamage * autocvar_g_balance_selfdamagepercent;
405         if(teamplay && actor.team)
406             desirabledamage = desirabledamage - teamdamage;
407
408         makevectors(actor.v_angle);
409         IL_EACH(g_mines, it.realowner == actor,
410         {
411             if(skill > 9) // normal players only do this for the target they are tracking
412             {
413                     entity mine = it;
414                     IL_EACH(g_bot_targets, it.bot_attack,
415                     {
416                         if((v_forward * normalize(mine.origin - it.origin) < 0.1)
417                             && desirabledamage > 0.1 * coredamage
418                             ) PHYS_INPUT_BUTTON_ATCK2(actor) = true;
419                     });
420                 }
421                 else
422                 {
423                 //As the distance gets larger, a correct detonation gets near imposible
424                 //Bots are assumed to use the mine spawnfunc_light to see if the mine gets near a player
425                 if((v_forward * normalize(it.origin - actor.enemy.origin) < 0.1)
426                         && IS_PLAYER(actor.enemy)
427                         && (desirabledamage >= 0.1 * coredamage)
428                         )
429                 {
430                         float distance = bound(300, vlen(actor.origin - actor.enemy.origin), 30000);
431                         if(random() / distance * 300 > frametime * bound(0, (10 - skill) * 0.2, 1))
432                                 PHYS_INPUT_BUTTON_ATCK2(actor) = true;
433                 }
434                 }
435         });
436
437         // if we would be doing at X percent of the core damage, detonate it
438         // but don't fire a new shot at the same time!
439         if(desirabledamage >= 0.75 * coredamage) //this should do group damage in rare fortunate events
440             PHYS_INPUT_BUTTON_ATCK2(actor) = true;
441         if((skill > 6.5) && (selfdamage > actor.health))
442             PHYS_INPUT_BUTTON_ATCK2(actor) = false;
443         //if(PHYS_INPUT_BUTTON_ATCK2(actor) == true)
444         //      dprint(ftos(desirabledamage),"\n");
445         if(PHYS_INPUT_BUTTON_ATCK2(actor)) PHYS_INPUT_BUTTON_ATCK(actor) = false;
446     }
447 }
448 METHOD(MineLayer, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
449 {
450         if(weaponslot(weaponentity) == 0)
451                 actor.minelayer_mines = actor.(weaponentity).minelayer_mines;
452
453     if(autocvar_g_balance_minelayer_reload_ammo && actor.(weaponentity).clip_load < WEP_CVAR(minelayer, ammo)) // forced reload
454     {
455         // not if we're holding the minelayer without enough ammo, but can detonate existing mines
456         if(!(W_MineLayer_PlacedMines(actor, weaponentity, false) && GetResourceAmount(actor, thiswep.ammo_type) < WEP_CVAR(minelayer, ammo))) {
457             thiswep.wr_reload(thiswep, actor, weaponentity);
458         }
459     }
460     else if(fire & 1)
461     {
462         if(weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR(minelayer, refire)))
463         {
464             W_MineLayer_Attack(thiswep, actor, weaponentity);
465             weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(minelayer, animtime), w_ready);
466         }
467     }
468
469     if(fire & 2)
470     {
471         if(W_MineLayer_PlacedMines(actor, weaponentity, true))
472             sound(actor, CH_WEAPON_B, SND_MINE_DET, VOL_BASE, ATTN_NORM);
473     }
474 }
475 METHOD(MineLayer, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
476 {
477     //int slot = 0; // TODO: unhardcode
478     // actually do // don't switch while placing a mine
479     //if(ATTACK_FINISHED(actor, slot) <= time || PS(actor).m_weapon != WEP_MINE_LAYER)
480     //{
481         float ammo_amount = GetResourceAmount(actor, thiswep.ammo_type) >= WEP_CVAR(minelayer, ammo);
482         ammo_amount += actor.(weaponentity).(weapon_load[WEP_MINE_LAYER.m_id]) >= WEP_CVAR(minelayer, ammo);
483         return ammo_amount;
484     //}
485     //return true;
486 }
487 METHOD(MineLayer, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
488 {
489     if(W_MineLayer_PlacedMines(actor, weaponentity, false))
490         return true;
491     else
492         return false;
493 }
494 METHOD(MineLayer, wr_resetplayer, void(entity thiswep, entity actor))
495 {
496     actor.minelayer_mines = 0;
497     for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
498     {
499         .entity weaponentity = weaponentities[slot];
500         actor.(weaponentity).minelayer_mines = 0;
501     }
502 }
503 METHOD(MineLayer, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
504 {
505     W_Reload(actor, weaponentity, WEP_CVAR(minelayer, ammo), SND_RELOAD);
506 }
507 METHOD(MineLayer, wr_suicidemessage, Notification(entity thiswep))
508 {
509     return WEAPON_MINELAYER_SUICIDE;
510 }
511 METHOD(MineLayer, wr_killmessage, Notification(entity thiswep))
512 {
513     return WEAPON_MINELAYER_MURDER;
514 }
515
516 #endif
517 #ifdef CSQC
518
519 METHOD(MineLayer, wr_impacteffect, void(entity thiswep, entity actor))
520 {
521     vector org2;
522     org2 = w_org + w_backoff * 12;
523     pointparticles(EFFECT_ROCKET_EXPLODE, org2, '0 0 0', 1);
524     if(!w_issilent)
525         sound(actor, CH_SHOTS, SND_MINE_EXP, VOL_BASE, ATTN_NORM);
526 }
527
528 #endif