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