Remove some more g_lms checks & add a use for mega health (disabled by default)
[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_PlayerSpawn)
22 {
23         if(IS_PLAYER(self))
24         if(restart_mapalreadyrestarted || (time < game_starttime))
25                 PlayerScore_Add(self, SP_LMS_LIVES, LMS_NewPlayerLives());
26                 
27         return FALSE;
28 }
29
30 MUTATOR_HOOKFUNCTION(lms_RemovePlayer)
31 {
32         // Only if the player cannot play at all
33         if(PlayerScore_Add(self, SP_LMS_RANK, 0) == 666)
34                 self.frags = FRAGS_SPECTATOR;
35         else
36                 self.frags = FRAGS_LMS_LOSER;
37                 
38         if(self.killcount != -666)
39                 if(PlayerScore_Add(self, SP_LMS_RANK, 0) > 0 && self.lms_spectate_warning != 2)
40                         Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_LMS_NOLIVES, self.netname);
41                 else
42                         Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_LMS_FORFEIT, self.netname);
43                 
44         return FALSE;
45 }
46
47 MUTATOR_HOOKFUNCTION(lms_ClientConnect)
48 {
49         self.classname = "player";
50         campaign_bots_may_start = 1;
51         
52         if(PlayerScore_Add(self, SP_LMS_LIVES, LMS_NewPlayerLives()) <= 0)
53         {
54                 PlayerScore_Add(self, SP_LMS_RANK, 666);
55                 self.frags = FRAGS_SPECTATOR;
56         }
57                         
58         return FALSE;
59 }
60
61 MUTATOR_HOOKFUNCTION(lms_PlayerThink)
62 {
63         if(self.deadflag == DEAD_DYING)
64                 self.deadflag = DEAD_RESPAWNING;
65                 
66         if not(self.deadflag)
67         if(autocvar_g_lms_campcheck_interval)
68         {
69                 vector dist;
70
71                 // calculate player movement (in 2 dimensions only, so jumping on one spot doesn't count as movement)
72                 dist = self.prevorigin - self.origin;
73                 dist_z = 0;
74                 self.lms_traveled_distance += fabs(vlen(dist));
75
76                 if((autocvar_g_campaign && !campaign_bots_may_start) || (time < game_starttime))
77                 {
78                         self.lms_nextcheck = time + autocvar_g_lms_campcheck_interval*2;
79                         self.lms_traveled_distance = 0;
80                 }
81
82                 if(time > self.lms_nextcheck)
83                 {
84                         if(self.lms_traveled_distance < autocvar_g_lms_campcheck_distance)
85                         {
86                                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_LMS_CAMPCHECK);
87                                 if(self.vehicle)
88                                         Damage(self.vehicle, self, self, autocvar_g_lms_campcheck_damage * 2, DEATH_CAMP, self.vehicle.origin, '0 0 0');
89                                 else
90                                         Damage(self, self, self, bound(0, autocvar_g_lms_campcheck_damage, self.health + self.armorvalue * autocvar_g_balance_armor_blockpercent + 5), DEATH_CAMP, self.origin, '0 0 0');
91                         }
92                         self.lms_nextcheck = time + autocvar_g_lms_campcheck_interval;
93                         self.lms_traveled_distance = 0;
94                 }
95         }
96                 
97         return FALSE;
98 }
99
100 MUTATOR_HOOKFUNCTION(lms_PlayerDamage)
101 {
102         if(IS_PLAYER(frag_target))
103         if(IS_PLAYER(frag_attacker))
104         if(frag_attacker != frag_target)
105         {
106                 frag_target.lms_traveled_distance = autocvar_g_lms_campcheck_distance;
107                 frag_attacker.lms_traveled_distance = autocvar_g_lms_campcheck_distance;
108         }
109                 
110         return FALSE;
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         string s;
143         
144         s = cvar_string("g_weaponarena");
145         if (s == "0" || s == "")
146                 s = "most";
147         
148         start_items &~= IT_UNLIMITED_AMMO;
149         start_ammo_shells = cvar("g_lms_start_ammo_shells");
150         start_ammo_nails = cvar("g_lms_start_ammo_nails");
151         start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
152         start_ammo_cells = cvar("g_lms_start_ammo_cells");
153         start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
154         start_health = cvar("g_lms_start_health");
155         start_armorvalue = cvar("g_lms_start_armor");
156
157         return FALSE;
158 }
159
160 MUTATOR_HOOKFUNCTION(lms_KeepScore)
161 {
162         // don't clear player score
163         return TRUE;
164 }
165
166 MUTATOR_HOOKFUNCTION(lms_FilterItem)
167 {
168         if(autocvar_g_lms_extra_lives)
169         if(self.classname == "item_health_mega")
170         {
171                 self.max_health = 1;
172                 return FALSE;
173         }
174         
175         return TRUE;
176 }
177
178 MUTATOR_HOOKFUNCTION(lms_ItemTouch)
179 {
180         // give extra lives for mega health
181         if(self.items & IT_HEALTH)
182                 PlayerScore_Add(other, SP_LMS_LIVES, autocvar_g_lms_extra_lives);
183         
184         return FALSE;
185 }
186
187 MUTATOR_HOOKFUNCTION(lms_BotSpawn)
188 {
189         // temporary hack to give bots lives
190         if(PlayerScore_Add(self, SP_LMS_LIVES, LMS_NewPlayerLives()) <= 0)
191         {
192                 PlayerScore_Add(self, SP_LMS_RANK, 666);
193                 self.frags = FRAGS_SPECTATOR;
194         }
195         
196         return FALSE;
197 }
198
199 // scoreboard stuff
200 void lms_ScoreRules()
201 {
202         ScoreRules_basics(0, 0, 0, FALSE);
203         ScoreInfo_SetLabel_PlayerScore(SP_LMS_LIVES,    "lives",     SFL_SORT_PRIO_SECONDARY);
204         ScoreInfo_SetLabel_PlayerScore(SP_LMS_RANK,     "rank",      SFL_LOWER_IS_BETTER | SFL_RANK | SFL_SORT_PRIO_PRIMARY | SFL_ALLOW_HIDE);
205         ScoreRules_basics_end();
206 }
207
208 void lms_Initialize()
209 {
210         lms_lowest_lives = 9999;
211         lms_next_place = 0;
212         
213         lms_ScoreRules();
214 }
215
216 MUTATOR_DEFINITION(gamemode_lms)
217 {
218         MUTATOR_HOOK(PlayerSpawn, lms_PlayerSpawn, CBC_ORDER_ANY);
219         MUTATOR_HOOK(MakePlayerObserver, lms_RemovePlayer, CBC_ORDER_ANY);
220         MUTATOR_HOOK(ClientConnect, lms_ClientConnect, CBC_ORDER_ANY);
221         MUTATOR_HOOK(PlayerPreThink, lms_PlayerThink, CBC_ORDER_ANY);
222         MUTATOR_HOOK(PlayerDamage_Calculate, lms_PlayerDamage, CBC_ORDER_ANY);
223         MUTATOR_HOOK(ForbidThrowCurrentWeapon, lms_ForbidThrowing, CBC_ORDER_ANY);
224         MUTATOR_HOOK(GiveFragsForKill, lms_GiveFragsForKill, CBC_ORDER_ANY);
225         MUTATOR_HOOK(SetStartItems, lms_SetStartItems, CBC_ORDER_ANY);
226         MUTATOR_HOOK(PlayerClearScore, lms_KeepScore, CBC_ORDER_ANY);
227         MUTATOR_HOOK(FilterItem, lms_FilterItem, CBC_ORDER_ANY);
228         MUTATOR_HOOK(ItemTouch, lms_ItemTouch, CBC_ORDER_ANY);
229         MUTATOR_HOOK(HavocBot_ChooseRule, lms_BotSpawn, CBC_ORDER_ANY);
230
231         MUTATOR_ONADD
232         {
233                 if(time > 1) // game loads at time 1
234                         error("This is a game type and it cannot be added at runtime.");
235                 lms_Initialize();
236         }
237
238         MUTATOR_ONROLLBACK_OR_REMOVE
239         {
240                 // we actually cannot roll back lms_Initialize here
241                 // BUT: we don't need to! If this gets called, adding always
242                 // succeeds.
243         }
244
245         MUTATOR_ONREMOVE
246         {
247                 print("This is a game type and it cannot be removed at runtime.");
248                 return -1;
249         }
250
251         return 0;
252 }