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