]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_invasion.qc
Merge branch 'master' into Mario/monsters
[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                 {
74                         if(head.weaponentity) remove(head.weaponentity);
75                         if(head.iceblock) remove(head.iceblock);
76                         WaypointSprite_Kill(head.sprite);
77                         remove(head);
78                 }
79                 
80                 if(roundcnt >= maxrounds)
81                 {
82                         NextLevel();
83                         return 1;
84                 }
85         
86                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_OVER);
87                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_OVER);
88                 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
89                 return 1;
90         }
91         
92         // boss round
93         if(roundcnt >= maxrounds)
94         {
95                 if(numspawned < 1)
96                 {
97                         maxspawned = 1;
98                         invasion_SpawnMonsters(0);
99                 }
100         }
101         else
102         {
103                 float total_alive_monsters = 0, supermonster_count = 0;
104                 
105                 FOR_EACH_MONSTER(head) if(head.health > 0)
106                 {
107                         if((get_monsterinfo(head.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
108                                 ++supermonster_count;
109                         ++total_alive_monsters;
110                 }
111
112                 if((total_alive_monsters + numkilled) < maxspawned && maxcurrent < 10) // 10 at a time should be plenty
113                 {
114                         if(time >= last_check)
115                         {
116                                 invasion_SpawnMonsters(supermonster_count);
117                                 last_check = time + 2;
118                         }
119                         
120                         return 0;
121                 }
122         }
123         
124         if(numspawned < 1 || numkilled < maxspawned)
125                 return 0; // nothing has spawned yet, or there are still alive monsters
126         
127         if(roundcnt >= maxrounds)
128         {
129                 NextLevel();
130                 return 1;
131         }
132         
133         entity winner = world;
134         float winning_score = 0;
135         
136         FOR_EACH_PLAYER(head)
137         {
138                 float cs = PlayerScore_Add(head, SP_KILLS, 0);
139                 if(cs > winning_score)
140                 {
141                         winning_score = cs;
142                         winner = head;
143                 }
144         }
145
146         if(winner)
147         {
148                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
149                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
150         }
151         
152         round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
153
154         return 1;
155 }
156
157 float Invasion_CheckPlayers()
158 {
159         return TRUE;
160 }
161
162 void Invasion_RoundStart()
163 {
164         entity e;
165         float numplayers = 0;
166         FOR_EACH_PLAYER(e)
167         {
168                 e.player_blocked = 0;
169                 ++numplayers;
170         }
171                 
172         roundcnt += 1;
173         
174         maxcurrent = 0;
175         numspawned = 0;
176         numkilled = 0;
177                 
178         if(roundcnt > 1)
179                 maxspawned = rint(autocvar_g_invasion_monster_count * (roundcnt * 0.5));
180         else
181                 maxspawned = autocvar_g_invasion_monster_count;
182         
183         monster_skill += 0.1 * numplayers;
184 }
185
186 MUTATOR_HOOKFUNCTION(invasion_MonsterDies)
187 {
188         if not(self.monster_respawned)
189         {
190                 numkilled += 1;
191                 maxcurrent -= 1;
192         
193                 if(IS_PLAYER(frag_attacker))
194                         PlayerScore_Add(frag_attacker, SP_KILLS, +1);
195         }
196         
197         return FALSE;
198 }
199
200 MUTATOR_HOOKFUNCTION(invasion_MonsterSpawn)
201 {
202         if not(self.spawnflags & MONSTERFLAG_SPAWNED)
203         {
204                 if(self.weaponentity) remove(self.weaponentity);
205                 if(self.iceblock) remove(self.iceblock);
206                 WaypointSprite_Kill(self.sprite);
207                 remove(self);
208                 return FALSE;
209         }
210         
211         if(roundcnt < maxrounds && self.spawnflags & MONSTERFLAG_MINIBOSS)
212                 self.spawnflags &= ~MONSTERFLAG_MINIBOSS;
213         
214         if not(self.monster_respawned)
215         {
216                 numspawned += 1;
217                 maxcurrent += 1;
218         }
219         
220         self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
221         
222         return FALSE;
223 }
224
225 MUTATOR_HOOKFUNCTION(invasion_PlayerThink)
226 {
227         monsters_total = maxspawned; // TODO: make sure numspawned never exceeds maxspawned
228         monsters_killed = numkilled;
229         
230         return FALSE;
231 }
232
233 MUTATOR_HOOKFUNCTION(invasion_PlayerSpawn)
234 {
235         self.bot_attack = FALSE;
236         return FALSE;
237 }
238
239 MUTATOR_HOOKFUNCTION(invasion_PlayerDamage)
240 {
241         if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
242         {
243                 frag_damage = 0;
244                 frag_force = '0 0 0';
245         }
246         
247         return FALSE;
248 }
249
250 MUTATOR_HOOKFUNCTION(invasion_PlayerCommand)
251 {
252         if(MUTATOR_RETURNVALUE) // command was already handled?
253                 return FALSE;
254         
255         if(cmd_name == "debuginvasion")
256         {
257                 sprint(self, strcat("maxspawned = ", ftos(maxspawned), "\n"));
258                 sprint(self, strcat("numspawned = ", ftos(numspawned), "\n"));
259                 sprint(self, strcat("numkilled = ", ftos(numkilled), "\n"));
260                 sprint(self, strcat("roundcnt = ", ftos(roundcnt), "\n"));
261                 sprint(self, strcat("monsters_total = ", ftos(monsters_total), "\n"));
262                 sprint(self, strcat("monsters_killed = ", ftos(monsters_killed), "\n"));
263                 sprint(self, strcat("monster_skill = ", ftos(monster_skill), "\n"));
264                 
265                 return TRUE;
266         }
267         
268         return FALSE;
269 }
270
271 MUTATOR_HOOKFUNCTION(invasion_SetStartItems)
272 {
273         start_health = 200;
274         start_armorvalue = 200;
275         
276         return FALSE;
277 }
278
279 void invasion_ScoreRules()
280 {
281         ScoreRules_basics(0, 0, 0, FALSE);
282         ScoreInfo_SetLabel_PlayerScore(SP_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
283         ScoreRules_basics_end();
284 }
285
286 void invasion_Initialize()
287 {
288         independent_players = 1; // to disable extra useless scores
289
290         invasion_ScoreRules();
291         
292         independent_players = 0;
293
294         round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
295         round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
296         
297         allowed_to_spawn = TRUE;
298         
299         monster_skill = 0.5;
300         
301         roundcnt = 0;
302 }
303
304 MUTATOR_DEFINITION(gamemode_invasion)
305 {
306         MUTATOR_HOOK(MonsterDies, invasion_MonsterDies, CBC_ORDER_ANY);
307         MUTATOR_HOOK(MonsterSpawn, invasion_MonsterSpawn, CBC_ORDER_ANY);
308         MUTATOR_HOOK(PlayerPreThink, invasion_PlayerThink, CBC_ORDER_ANY);
309         MUTATOR_HOOK(PlayerSpawn, invasion_PlayerSpawn, CBC_ORDER_ANY);
310         MUTATOR_HOOK(PlayerDamage_Calculate, invasion_PlayerDamage, CBC_ORDER_ANY);
311         MUTATOR_HOOK(SV_ParseClientCommand, invasion_PlayerCommand, CBC_ORDER_ANY);
312         MUTATOR_HOOK(SetStartItems, invasion_SetStartItems, CBC_ORDER_ANY);
313         
314         MUTATOR_ONADD
315         {
316                 if(time > 1) // game loads at time 1
317                         error("This is a game type and it cannot be added at runtime.");
318                 invasion_Initialize();
319                 
320                 cvar_settemp("g_monsters", "1");
321         }
322
323         MUTATOR_ONROLLBACK_OR_REMOVE
324         {
325                 // we actually cannot roll back invasion_Initialize here
326                 // BUT: we don't need to! If this gets called, adding always
327                 // succeeds.
328         }
329
330         MUTATOR_ONREMOVE
331         {
332                 print("This is a game type and it cannot be removed at runtime.");
333                 return -1;
334         }
335
336         return 0;
337 }