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