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