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