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