Merge branch 'master' into Mario/vaporizer_damage
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / gamemode_lms.qc
1 #include "gamemode_lms.qh"
2 #include "../_all.qh"
3
4 #include "gamemode.qh"
5
6 #include "../campaign.qh"
7 #include "../command/cmd.qh"
8
9 // main functions
10 float LMS_NewPlayerLives()
11 {
12         float fl;
13         fl = autocvar_fraglimit;
14         if(fl == 0)
15                 fl = 999;
16
17         // first player has left the game for dying too much? Nobody else can get in.
18         if(lms_lowest_lives < 1)
19                 return 0;
20
21         if(!autocvar_g_lms_join_anytime)
22                 if(lms_lowest_lives < fl - autocvar_g_lms_last_join)
23                         return 0;
24
25         return bound(1, lms_lowest_lives, fl);
26 }
27
28 // mutator hooks
29 MUTATOR_HOOKFUNCTION(lms_ResetMap)
30 {
31         lms_lowest_lives = 999;
32         lms_next_place = player_count;
33
34         return false;
35 }
36
37 MUTATOR_HOOKFUNCTION(lms_ResetPlayers)
38 {
39         if(restart_mapalreadyrestarted || (time < game_starttime))
40         FOR_EACH_CLIENT(self)
41         if(IS_PLAYER(self))
42                 PlayerScore_Add(self, SP_LMS_LIVES, LMS_NewPlayerLives());
43
44         return false;
45 }
46
47 MUTATOR_HOOKFUNCTION(lms_PlayerPreSpawn)
48 {
49         // player is dead and becomes observer
50         // FIXME fix LMS scoring for new system
51         if(PlayerScore_Add(self, SP_LMS_RANK, 0) > 0)
52         {
53                 self.classname = "observer";
54                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_LMS_NOLIVES);
55         }
56
57         return false;
58 }
59
60 MUTATOR_HOOKFUNCTION(lms_PlayerDies)
61 {
62         self.respawn_flags |= RESPAWN_FORCE;
63
64         return false;
65 }
66
67 MUTATOR_HOOKFUNCTION(lms_RemovePlayer)
68 {
69         // Only if the player cannot play at all
70         if(PlayerScore_Add(self, SP_LMS_RANK, 0) == 666)
71                 self.frags = FRAGS_SPECTATOR;
72         else
73                 self.frags = FRAGS_LMS_LOSER;
74
75         if(self.killcount != -666)
76                 if(PlayerScore_Add(self, SP_LMS_RANK, 0) > 0 && self.lms_spectate_warning != 2)
77                         Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_LMS_NOLIVES, self.netname);
78                 else
79                         Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_LMS_FORFEIT, self.netname);
80
81         return false;
82 }
83
84 MUTATOR_HOOKFUNCTION(lms_ClientConnect)
85 {
86         self.classname = "player";
87         campaign_bots_may_start = 1;
88
89         if(PlayerScore_Add(self, SP_LMS_LIVES, LMS_NewPlayerLives()) <= 0)
90         {
91                 PlayerScore_Add(self, SP_LMS_RANK, 666);
92                 self.frags = FRAGS_SPECTATOR;
93         }
94
95         return false;
96 }
97
98 MUTATOR_HOOKFUNCTION(lms_PlayerThink)
99 {
100         if(self.deadflag == DEAD_DYING)
101                 self.deadflag = DEAD_RESPAWNING;
102
103         return false;
104 }
105
106 MUTATOR_HOOKFUNCTION(lms_PlayerRegen)
107 {
108         if(autocvar_g_lms_regenerate)
109                 return false;
110         return true;
111 }
112
113 MUTATOR_HOOKFUNCTION(lms_ForbidThrowing)
114 {
115         // forbode!
116         return true;
117 }
118
119 MUTATOR_HOOKFUNCTION(lms_GiveFragsForKill)
120 {
121         // remove a life
122         float tl;
123         tl = PlayerScore_Add(frag_target, SP_LMS_LIVES, -1);
124         if(tl < lms_lowest_lives)
125                 lms_lowest_lives = tl;
126         if(tl <= 0)
127         {
128                 if(!lms_next_place)
129                         lms_next_place = player_count;
130                 else
131                         lms_next_place = min(lms_next_place, player_count);
132                 PlayerScore_Add(frag_target, SP_LMS_RANK, lms_next_place); // won't ever spawn again
133                 --lms_next_place;
134         }
135         frag_score = 0;
136
137         return true;
138 }
139
140 MUTATOR_HOOKFUNCTION(lms_SetStartItems)
141 {
142         start_items &= ~IT_UNLIMITED_AMMO;
143         start_health       = warmup_start_health       = cvar("g_lms_start_health");
144         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
145         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
146         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
147         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
148         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
149         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
150         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
151
152         return false;
153 }
154
155 MUTATOR_HOOKFUNCTION(lms_KeepScore)
156 {
157         // don't clear player score
158         return true;
159 }
160
161 MUTATOR_HOOKFUNCTION(lms_FilterItem)
162 {
163         if(autocvar_g_lms_extra_lives)
164         if(self.itemdef == ITEM_HealthMega)
165         {
166                 self.max_health = 1;
167                 return false;
168         }
169
170         return true;
171 }
172
173 MUTATOR_HOOKFUNCTION(lms_ItemTouch)
174 {
175         // give extra lives for mega health
176         if (self.items & ITEM_HealthMega.m_itemid)
177         {
178                 Send_Notification(NOTIF_ONE, other, MSG_CENTER, CENTER_EXTRALIVES);
179                 PlayerScore_Add(other, SP_LMS_LIVES, autocvar_g_lms_extra_lives);
180         }
181
182         return MUT_ITEMTOUCH_CONTINUE;
183 }
184
185 // scoreboard stuff
186 void lms_ScoreRules()
187 {
188         ScoreRules_basics(0, 0, 0, false);
189         ScoreInfo_SetLabel_PlayerScore(SP_LMS_LIVES,    "lives",     SFL_SORT_PRIO_SECONDARY);
190         ScoreInfo_SetLabel_PlayerScore(SP_LMS_RANK,     "rank",      SFL_LOWER_IS_BETTER | SFL_RANK | SFL_SORT_PRIO_PRIMARY | SFL_ALLOW_HIDE);
191         ScoreRules_basics_end();
192 }
193
194 void lms_Initialize()
195 {
196         lms_lowest_lives = 9999;
197         lms_next_place = 0;
198
199         lms_ScoreRules();
200 }
201
202 MUTATOR_DEFINITION(gamemode_lms)
203 {
204         MUTATOR_HOOK(reset_map_global, lms_ResetMap, CBC_ORDER_ANY);
205         MUTATOR_HOOK(reset_map_players, lms_ResetPlayers, CBC_ORDER_ANY);
206         MUTATOR_HOOK(PutClientInServer, lms_PlayerPreSpawn, CBC_ORDER_ANY);
207         MUTATOR_HOOK(PlayerDies, lms_PlayerDies, CBC_ORDER_ANY);
208         MUTATOR_HOOK(MakePlayerObserver, lms_RemovePlayer, CBC_ORDER_ANY);
209         MUTATOR_HOOK(ClientConnect, lms_ClientConnect, CBC_ORDER_ANY);
210         MUTATOR_HOOK(PlayerPreThink, lms_PlayerThink, CBC_ORDER_ANY);
211         MUTATOR_HOOK(PlayerRegen, lms_PlayerRegen, CBC_ORDER_ANY);
212         MUTATOR_HOOK(ForbidThrowCurrentWeapon, lms_ForbidThrowing, CBC_ORDER_ANY);
213         MUTATOR_HOOK(GiveFragsForKill, lms_GiveFragsForKill, CBC_ORDER_ANY);
214         MUTATOR_HOOK(SetStartItems, lms_SetStartItems, CBC_ORDER_ANY);
215         MUTATOR_HOOK(ForbidPlayerScore_Clear, lms_KeepScore, CBC_ORDER_ANY);
216         MUTATOR_HOOK(FilterItem, lms_FilterItem, CBC_ORDER_ANY);
217         MUTATOR_HOOK(ItemTouch, lms_ItemTouch, CBC_ORDER_ANY);
218
219         MUTATOR_ONADD
220         {
221                 if(time > 1) // game loads at time 1
222                         error("This is a game type and it cannot be added at runtime.");
223                 lms_Initialize();
224         }
225
226         MUTATOR_ONROLLBACK_OR_REMOVE
227         {
228                 // we actually cannot roll back lms_Initialize here
229                 // BUT: we don't need to! If this gets called, adding always
230                 // succeeds.
231         }
232
233         MUTATOR_ONREMOVE
234         {
235                 LOG_INFO("This is a game type and it cannot be removed at runtime.");
236                 return -1;
237         }
238
239         return 0;
240 }