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