Remove useless weaponarena check
[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         start_items &~= IT_UNLIMITED_AMMO;
143         start_ammo_shells = cvar("g_lms_start_ammo_shells");
144         start_ammo_nails = cvar("g_lms_start_ammo_nails");
145         start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
146         start_ammo_cells = cvar("g_lms_start_ammo_cells");
147         start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
148         start_health = cvar("g_lms_start_health");
149         start_armorvalue = cvar("g_lms_start_armor");
150
151         return FALSE;
152 }
153
154 MUTATOR_HOOKFUNCTION(lms_KeepScore)
155 {
156         // don't clear player score
157         return TRUE;
158 }
159
160 MUTATOR_HOOKFUNCTION(lms_FilterItem)
161 {
162         if(autocvar_g_lms_extra_lives)
163         if(self.classname == "item_health_mega")
164         {
165                 self.max_health = 1;
166                 return FALSE;
167         }
168         
169         return TRUE;
170 }
171
172 MUTATOR_HOOKFUNCTION(lms_ItemTouch)
173 {
174         // give extra lives for mega health
175         if(self.items & IT_HEALTH)
176         {
177                 Send_Notification(NOTIF_ONE, other, MSG_CENTER, CENTER_EXTRALIVES);
178                 PlayerScore_Add(other, SP_LMS_LIVES, autocvar_g_lms_extra_lives);
179         }
180         
181         return FALSE;
182 }
183
184 MUTATOR_HOOKFUNCTION(lms_BotSpawn)
185 {
186         // temporary hack to give bots lives
187         if(PlayerScore_Add(self, SP_LMS_LIVES, LMS_NewPlayerLives()) <= 0)
188         {
189                 PlayerScore_Add(self, SP_LMS_RANK, 666);
190                 self.frags = FRAGS_SPECTATOR;
191         }
192         
193         return FALSE;
194 }
195
196 // scoreboard stuff
197 void lms_ScoreRules()
198 {
199         ScoreRules_basics(0, 0, 0, FALSE);
200         ScoreInfo_SetLabel_PlayerScore(SP_LMS_LIVES,    "lives",     SFL_SORT_PRIO_SECONDARY);
201         ScoreInfo_SetLabel_PlayerScore(SP_LMS_RANK,     "rank",      SFL_LOWER_IS_BETTER | SFL_RANK | SFL_SORT_PRIO_PRIMARY | SFL_ALLOW_HIDE);
202         ScoreRules_basics_end();
203 }
204
205 void lms_Initialize()
206 {
207         lms_lowest_lives = 9999;
208         lms_next_place = 0;
209         
210         lms_ScoreRules();
211 }
212
213 MUTATOR_DEFINITION(gamemode_lms)
214 {
215         MUTATOR_HOOK(PlayerSpawn, lms_PlayerSpawn, CBC_ORDER_ANY);
216         MUTATOR_HOOK(MakePlayerObserver, lms_RemovePlayer, CBC_ORDER_ANY);
217         MUTATOR_HOOK(ClientConnect, lms_ClientConnect, CBC_ORDER_ANY);
218         MUTATOR_HOOK(PlayerPreThink, lms_PlayerThink, CBC_ORDER_ANY);
219         MUTATOR_HOOK(PlayerDamage_Calculate, lms_PlayerDamage, CBC_ORDER_ANY);
220         MUTATOR_HOOK(ForbidThrowCurrentWeapon, lms_ForbidThrowing, CBC_ORDER_ANY);
221         MUTATOR_HOOK(GiveFragsForKill, lms_GiveFragsForKill, CBC_ORDER_ANY);
222         MUTATOR_HOOK(SetStartItems, lms_SetStartItems, CBC_ORDER_ANY);
223         MUTATOR_HOOK(PlayerClearScore, lms_KeepScore, CBC_ORDER_ANY);
224         MUTATOR_HOOK(FilterItem, lms_FilterItem, CBC_ORDER_ANY);
225         MUTATOR_HOOK(ItemTouch, lms_ItemTouch, CBC_ORDER_ANY);
226         MUTATOR_HOOK(HavocBot_ChooseRule, lms_BotSpawn, CBC_ORDER_ANY);
227
228         MUTATOR_ONADD
229         {
230                 if(time > 1) // game loads at time 1
231                         error("This is a game type and it cannot be added at runtime.");
232                 lms_Initialize();
233         }
234
235         MUTATOR_ONROLLBACK_OR_REMOVE
236         {
237                 // we actually cannot roll back lms_Initialize here
238                 // BUT: we don't need to! If this gets called, adding always
239                 // succeeds.
240         }
241
242         MUTATOR_ONREMOVE
243         {
244                 print("This is a game type and it cannot be removed at runtime.");
245                 return -1;
246         }
247
248         return 0;
249 }