]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_invasion.qc
Hide useless stats from the scoreboard
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / gamemode_invasion.qc
1 void invasion_spawnpoint()
2 {
3         if not(g_invasion) { remove(self); return; }
4         
5         self.classname = "invasion_spawnpoint";
6 }
7
8 float invasion_PickMonster()
9 {
10         if(autocvar_g_invasion_zombies_only)
11                 return MONSTER_ZOMBIE;
12
13         float i;
14         
15         RandomSelection_Init();
16         
17         for(i = MONSTER_FIRST + 1; i < MONSTER_LAST; ++i)
18         {
19                 if(i == MONSTER_STINGRAY || i == MONSTER_WYVERN)
20                         continue; // flying/swimming monsters not yet supported
21                 
22                 RandomSelection_Add(world, i, "", 1, 1);
23         }
24         
25         return RandomSelection_chosen_float;
26 }
27
28 entity invasion_PickSpawn()
29 {
30         entity e;
31         
32         RandomSelection_Init();
33         
34         for(e = world;(e = find(e, classname, "invasion_spawnpoint")); )
35                 RandomSelection_Add(e, 0, string_null, 1, 1);
36                 
37         return RandomSelection_chosen_ent;
38 }
39
40 void invasion_SpawnChosenMonster(float mon)
41 {
42         entity spawn_point, monster;
43         
44         spawn_point = invasion_PickSpawn();
45         
46         if(spawn_point == world)
47         {
48                 dprint("Warning: couldn't find any invasion_spawnpoint spawnpoints, no monsters will spawn!\n");
49                 return;
50         }
51         
52         monster = spawnmonster("", mon, spawn_point, spawn_point, spawn_point.origin, FALSE, 2);
53 }
54
55 void invasion_SpawnMonsters()
56 {
57         float chosen_monster = invasion_PickMonster();
58         
59         invasion_SpawnChosenMonster(chosen_monster);
60 }
61
62 float Invasion_CheckWinner()
63 {
64         entity head;
65         if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
66         {
67                 FOR_EACH_MONSTER(head)
68                 {
69                         WaypointSprite_Kill(head.sprite);
70                         if(head.weaponentity) remove(head.weaponentity);
71                         remove(head);
72                 }
73         
74                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_OVER);
75                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_OVER);
76                 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
77                 return 1;
78         }
79         
80         float total_alive_monsters = 0;
81         
82         FOR_EACH_MONSTER(head) if(head.health > 0)
83                 ++total_alive_monsters;
84
85         if((total_alive_monsters + numkilled) < maxspawned)
86         {
87                 if(time >= last_check)
88                 {
89                         invasion_SpawnMonsters();
90                         last_check = time + 2;
91                 }
92                 
93                 return 0;
94         }
95         
96         if(numspawned < 1 || numkilled < maxspawned)
97                 return 0; // nothing has spawned yet, or there are still alive monsters
98         
99         if(roundcnt >= maxrounds)
100         {
101                 NextLevel();
102                 return 1;
103         }
104         
105         entity winner = world;
106         float winning_score = 0;
107         
108         FOR_EACH_PLAYER(head)
109         {
110                 float cs = PlayerScore_Add(head, SP_KILLS, 0);
111                 if(cs > winning_score)
112                 {
113                         winning_score = cs;
114                         winner = head;
115                 }
116         }
117
118         if(winner)
119         {
120                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
121                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
122         }
123         
124         round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
125
126         return 1;
127 }
128
129 float Invasion_CheckPlayers()
130 {
131         return TRUE;
132 }
133
134 void Invasion_RoundStart()
135 {
136         entity e;
137         float numplayers = 0;
138         FOR_EACH_PLAYER(e)
139         {
140                 e.player_blocked = 0;
141                 ++numplayers;
142         }
143                 
144         roundcnt += 1;
145         
146         numspawned = 0;
147         numkilled = 0;
148                 
149         if(roundcnt > 1)
150                 maxspawned = rint(autocvar_g_invasion_monster_count * (roundcnt * 0.5));
151         else
152                 maxspawned = autocvar_g_invasion_monster_count;
153         
154         monster_skill += 0.1 * numplayers;
155 }
156
157 MUTATOR_HOOKFUNCTION(invasion_MonsterDies)
158 {
159         numkilled += 1;
160         
161         if(IS_PLAYER(frag_attacker))
162                 PlayerScore_Add(frag_attacker, SP_KILLS, +1);
163         
164         return FALSE;
165 }
166
167 MUTATOR_HOOKFUNCTION(invasion_MonsterSpawn)
168 {
169         if(self.realowner == world)
170         {
171                 WaypointSprite_Kill(self.sprite);
172                 if(self.weaponentity) remove(self.weaponentity);
173                 remove(self);
174                 return FALSE;
175         }
176         
177         numspawned += 1;
178         
179         self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
180         
181         return FALSE;
182 }
183
184 MUTATOR_HOOKFUNCTION(invasion_PlayerThink)
185 {
186         monsters_total = maxspawned; // TODO: make sure numspawned never exceeds maxspawned
187         monsters_killed = numkilled;
188         
189         return FALSE;
190 }
191
192 MUTATOR_HOOKFUNCTION(invasion_PlayerSpawn)
193 {
194         self.bot_attack = FALSE;
195         return FALSE;
196 }
197
198 MUTATOR_HOOKFUNCTION(invasion_PlayerDamage)
199 {
200         if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
201         {
202                 frag_damage = 0;
203                 frag_force = '0 0 0';
204         }
205         
206         if(frag_attacker.flags & FL_MONSTER && frag_target.flags & FL_MONSTER && frag_attacker != frag_target)
207                 frag_damage = 0;
208         
209         return FALSE;
210 }
211
212 MUTATOR_HOOKFUNCTION(invasion_PlayerCommand)
213 {
214         if(MUTATOR_RETURNVALUE) // command was already handled?
215                 return FALSE;
216         
217         if(cmd_name == "debuginvasion")
218         {
219                 sprint(self, strcat("maxspawned = ", ftos(maxspawned), "\n"));
220                 sprint(self, strcat("numspawned = ", ftos(numspawned), "\n"));
221                 sprint(self, strcat("numkilled = ", ftos(numkilled), "\n"));
222                 sprint(self, strcat("roundcnt = ", ftos(roundcnt), "\n"));
223                 sprint(self, strcat("monsters_total = ", ftos(monsters_total), "\n"));
224                 sprint(self, strcat("monsters_killed = ", ftos(monsters_killed), "\n"));
225                 sprint(self, strcat("monster_skill = ", ftos(monster_skill), "\n"));
226                 
227                 return TRUE;
228         }
229         
230         return FALSE;
231 }
232
233 void invasion_ScoreRules()
234 {
235         ScoreRules_basics(0, 0, 0, FALSE);
236         ScoreInfo_SetLabel_PlayerScore(SP_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
237         ScoreRules_basics_end();
238 }
239
240 void invasion_Initialize()
241 {
242         independent_players = 1; // to disable extra useless scores
243
244         invasion_ScoreRules();
245         
246         independent_players = 0;
247
248         round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
249         round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
250         
251         allowed_to_spawn = TRUE;
252         
253         monster_skill = 0.5;
254         
255         roundcnt = 0;
256 }
257
258 MUTATOR_DEFINITION(gamemode_invasion)
259 {
260         MUTATOR_HOOK(MonsterDies, invasion_MonsterDies, CBC_ORDER_ANY);
261         MUTATOR_HOOK(MonsterSpawn, invasion_MonsterSpawn, CBC_ORDER_ANY);
262         MUTATOR_HOOK(PlayerPreThink, invasion_PlayerThink, CBC_ORDER_ANY);
263         MUTATOR_HOOK(PlayerSpawn, invasion_PlayerSpawn, CBC_ORDER_ANY);
264         MUTATOR_HOOK(PlayerDamage_Calculate, invasion_PlayerDamage, CBC_ORDER_ANY);
265         MUTATOR_HOOK(SV_ParseClientCommand, invasion_PlayerCommand, CBC_ORDER_ANY);
266         
267         MUTATOR_ONADD
268         {
269                 if(time > 1) // game loads at time 1
270                         error("This is a game type and it cannot be added at runtime.");
271                 invasion_Initialize();
272                 
273                 cvar_settemp("g_monsters", "1");
274         }
275
276         MUTATOR_ONROLLBACK_OR_REMOVE
277         {
278                 // we actually cannot roll back invasion_Initialize here
279                 // BUT: we don't need to! If this gets called, adding always
280                 // succeeds.
281         }
282
283         MUTATOR_ONREMOVE
284         {
285                 print("This is a game type and it cannot be removed at runtime.");
286                 return -1;
287         }
288
289         return 0;
290 }