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