Merge branch 'master' into Mario/vaporizer_damage
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator_random_gravity.qc
1 #include "../_all.qh"
2
3 #include "mutator.qh"
4
5 // Random Gravity
6 //
7 // Mutator by Mario
8 // Inspired by Player 2
9
10 float gravity_delay;
11
12 MUTATOR_HOOKFUNCTION(gravity_StartFrame)
13 {
14         if(gameover || !cvar("g_random_gravity")) return false;
15         if(time < gravity_delay) return false;
16         if(time < game_starttime) return false;
17         if(round_handler_IsActive() && !round_handler_IsRoundStarted()) return false;
18
19     if(random() >= autocvar_g_random_gravity_negative_chance)
20         cvar_set("sv_gravity", ftos(bound(autocvar_g_random_gravity_min, random() - random() * -autocvar_g_random_gravity_negative, autocvar_g_random_gravity_max)));
21     else
22         cvar_set("sv_gravity", ftos(bound(autocvar_g_random_gravity_min, random() * autocvar_g_random_gravity_positive, autocvar_g_random_gravity_max)));
23
24         gravity_delay = time + autocvar_g_random_gravity_delay;
25
26         LOG_TRACE("Gravity is now: ", ftos(autocvar_sv_gravity), "\n");
27
28         return false;
29 }
30
31 MUTATOR_HOOKFUNCTION(gravity_BuildMutatorsString)
32 {
33         ret_string = strcat(ret_string, ":RandomGravity");
34         return 0;
35 }
36
37 MUTATOR_HOOKFUNCTION(gravity_BuildMutatorsPrettyString)
38 {
39         ret_string = strcat(ret_string, ", Random gravity");
40         return 0;
41 }
42
43 MUTATOR_DEFINITION(mutator_random_gravity)
44 {
45         MUTATOR_HOOK(SV_StartFrame, gravity_StartFrame, CBC_ORDER_ANY);
46         MUTATOR_HOOK(BuildMutatorsString, gravity_BuildMutatorsString, CBC_ORDER_ANY);
47         MUTATOR_HOOK(BuildMutatorsPrettyString, gravity_BuildMutatorsPrettyString, CBC_ORDER_ANY);
48
49         MUTATOR_ONADD
50         {
51                 cvar_settemp("sv_gravity", cvar_string("sv_gravity")); // settemp current gravity so it's restored on match end
52         }
53
54         return false;
55 }