make sure leadlimit is >= 0
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / campcheck / sv_campcheck.qc
1 #include "sv_campcheck.qh"
2
3 string autocvar_g_campcheck;
4 float autocvar_g_campcheck_damage;
5 float autocvar_g_campcheck_distance;
6 float autocvar_g_campcheck_interval;
7
8 REGISTER_MUTATOR(campcheck, expr_evaluate(autocvar_g_campcheck));
9
10 .float campcheck_nextcheck;
11 .float campcheck_traveled_distance;
12
13 .vector campcheck_prevorigin;
14
15 MUTATOR_HOOKFUNCTION(campcheck, PlayerDies)
16 {
17         entity frag_target = M_ARGV(2, entity);
18
19         Kill_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CPID_CAMPCHECK);
20 }
21
22 MUTATOR_HOOKFUNCTION(campcheck, Damage_Calculate)
23 {
24         entity frag_attacker = M_ARGV(1, entity);
25         entity frag_target = M_ARGV(2, entity);
26
27         if(IS_PLAYER(frag_target))
28         if(IS_PLAYER(frag_attacker))
29         if(frag_attacker != frag_target)
30         {
31                 frag_target.campcheck_traveled_distance = autocvar_g_campcheck_distance;
32                 frag_attacker.campcheck_traveled_distance = autocvar_g_campcheck_distance;
33         }
34 }
35
36 MUTATOR_HOOKFUNCTION(campcheck, PlayerPreThink)
37 {
38         entity player = M_ARGV(0, entity);
39         bool checked = false;
40
41         if(autocvar_g_campcheck_interval)
42         if(!game_stopped && !warmup_stage && time >= game_starttime)
43         if(IS_PLAYER(player))
44         if(!IS_DEAD(player))
45         if(!STAT(FROZEN, player))
46         if(!PHYS_INPUT_BUTTON_CHAT(player))
47         if(IS_REAL_CLIENT(player)) // bots may camp, but that's no reason to constantly kill them
48         if(!weaponLocked(player))
49         {
50                 // calculate player movement (in 2 dimensions only, so jumping on one spot doesn't count as movement)
51                 vector dist = vec2(player.campcheck_prevorigin - player.origin);
52                 player.campcheck_traveled_distance += fabs(vlen(dist));
53
54                 if((autocvar_g_campaign && !campaign_bots_may_start) || (time < game_starttime) || (round_handler_IsActive() && !round_handler_IsRoundStarted()))
55                 {
56                         player.campcheck_nextcheck = time + autocvar_g_campcheck_interval * 2;
57                         player.campcheck_traveled_distance = 0;
58                 }
59
60                 if(time > player.campcheck_nextcheck)
61                 {
62                         if(player.campcheck_traveled_distance < autocvar_g_campcheck_distance)
63                         {
64                                 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_CAMPCHECK);
65                                 if(player.vehicle)
66                                         Damage(player.vehicle, NULL, NULL, autocvar_g_campcheck_damage * 2, DEATH_CAMP.m_id, DMG_NOWEP, player.vehicle.origin, '0 0 0');
67                                 else
68                                         Damage(player, NULL, NULL, bound(0, autocvar_g_campcheck_damage, GetResource(player, RES_HEALTH) + GetResource(player, RES_ARMOR) * autocvar_g_balance_armor_blockpercent + 5), DEATH_CAMP.m_id, DMG_NOWEP, player.origin, '0 0 0');
69                         }
70                         player.campcheck_nextcheck = time + autocvar_g_campcheck_interval;
71                         player.campcheck_traveled_distance = 0;
72                 }
73
74                 checked = true;
75         }
76
77         if(!checked)
78                 player.campcheck_nextcheck = time + autocvar_g_campcheck_interval; // one of the above checks failed, so keep the timer up to date
79
80         player.campcheck_prevorigin = player.origin;
81 }
82
83 MUTATOR_HOOKFUNCTION(campcheck, CopyBody)
84 {
85         entity player = M_ARGV(0, entity);
86         entity clone = M_ARGV(1, entity);
87
88         clone.campcheck_prevorigin = player.campcheck_prevorigin;
89 }
90
91 MUTATOR_HOOKFUNCTION(campcheck, PlayerSpawn)
92 {
93         entity player = M_ARGV(0, entity);
94
95         player.campcheck_nextcheck = time + autocvar_g_campcheck_interval * 2;
96         player.campcheck_traveled_distance = 0;
97 }
98
99 MUTATOR_HOOKFUNCTION(campcheck, BuildMutatorsString)
100 {
101         M_ARGV(0, string) = strcat(M_ARGV(0, string), ":CampCheck");
102 }