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