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