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