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