]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/mmm/sv_mmm.qc
Added new cvar player ammo start (bullets, rockets, cells) and deleted Sleuth skill...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / gamemodes / gamemode / mmm / sv_mmm.qc
1 #include "sv_mmm.qh"
2
3 //set g_mmm_sleuth_count 0.125 "number of players who will become sleuths, set between 0 and 0.9 to use a multiplier of the current players, or 1 and above to specify an exact number of players"
4 //float autocvar_g_mmm_sleuth_count = 0.125; //I don't think that it won't be used...
5 float autocvar_g_mmm_civilian_count = 0.625;
6 //float autocvar_g_mmm_murderer_count = 0.25;
7 float autocvar_g_mmm_round_timelimit = 180;
8 float autocvar_g_mmm_warmup = 10;
9 bool autocvar_g_mmm_punish_teamkill = false;
10 bool autocvar_g_mmm_reward_civilian = true;
11 bool autocvar_g_mmm_reward_sleuth = true; //sleuth reward if investigated corpses
12 float autocvar_g_mmm_max_karma_points = 1000; //LegendGuard sets Karma points 21-02-2021
13 float autocvar_g_mmm_min_karma_points = 400;
14 int autocvar_g_mmm_karma_bankick_tool = 0; //LegendGuard sets a ban tool for server admins 11-03-2021
15 float autocvar_g_mmm_karma_bantime = 1800; //karma ban seconds
16 bool autocvar_g_mmm_karma_damageactive = true; //LegendGuard sets Karma damage setting if active 20-03-2021
17 float autocvar_g_mmm_karma_severity = 0.25;
18 float autocvar_g_mmm_karma_damagepunishmentdeal = 20; //LegendGuard sets Karma punishment damage setting if player kills an ally 28-03-2021
19 int autocvar_g_mmm_playerammobrcstartquantity = 20;
20 // Sleuth is a created team, this team is added inside Civilians team
21
22 void mmm_FakeTimeLimit(entity e, float t)
23 {
24         if(!IS_REAL_CLIENT(e))
25                 return;
26 #if 0
27         msg_entity = e;
28         WriteByte(MSG_ONE, 3); // svc_updatestat
29         WriteByte(MSG_ONE, 236); // STAT_TIMELIMIT
30         if(t < 0)
31                 WriteCoord(MSG_ONE, autocvar_timelimit);
32         else
33                 WriteCoord(MSG_ONE, (t + 1) / 60);
34 #else
35         STAT(MMM_ROUNDTIMER, e) = t;
36 #endif
37 }
38
39 void nades_Clear(entity player);
40
41 void karma_Control(entity it)
42 {
43         float masksize = autocvar_g_ban_default_masksize;
44         float bantime = autocvar_g_mmm_karma_bantime;
45         if(it.karmapoints >= autocvar_g_mmm_max_karma_points)
46         {
47                 //Resets karmapoints to maintain the maximum
48                 //PrintToChatAll("^3REWARD ^1MAXIMUM RESET");
49                 GameRules_scoring_add(it, MMM_KARMA, autocvar_g_mmm_max_karma_points - it.karmapoints);
50                 it.karmapoints = autocvar_g_mmm_max_karma_points;
51         }
52         else if(it.karmapoints <= autocvar_g_mmm_min_karma_points)
53         {
54                 switch (autocvar_g_mmm_karma_bankick_tool)
55                 {
56                         //force to spec
57                         case 0: PutObserverInServer(it); return;
58                         //kick
59                         case 1: dropclient(it); return;
60                         //ban and kick
61                         case 2: Ban_KickBanClient(it, bantime, masksize, "Too low karma"); return;
62                         //force to spec
63                         default: PutObserverInServer(it); return;
64                 }
65         }
66 }
67
68 void karmaLoseDifference(entity attacker, entity target)
69 {
70         if (autocvar_g_mmm_karma_severity <= 0.09)
71                 autocvar_g_mmm_karma_severity = 0.1;
72         else if (autocvar_g_mmm_karma_severity > 1)
73                 autocvar_g_mmm_karma_severity = 1;
74         
75         //BASIC MATH THEORY: example: 1000 * 0.3 * (0.1 + 0.4) * 0.25 // karma points reduce when player attacked to other player
76         if (target.karmapoints < attacker.karmapoints)
77         {
78                 float decreasekarma = - ( target.karmapoints * random() * ( 0.1 + random() ) * autocvar_g_mmm_karma_severity );
79                 GameRules_scoring_add(attacker, MMM_KARMA, decreasekarma);
80                 attacker.karmapoints = attacker.karmapoints + decreasekarma;
81         }
82         else if (target.karmapoints > attacker.karmapoints)
83         {
84                 float decreasekarma = - ( target.karmapoints * random() * ( 0.1 + random() ) * autocvar_g_mmm_karma_severity );
85                 GameRules_scoring_add(attacker, MMM_KARMA, decreasekarma);
86                 attacker.karmapoints = attacker.karmapoints + decreasekarma;
87         }
88         else
89         {
90                 float decreasekarma = - ( target.karmapoints * random() * ( 0.1 + random() ) * autocvar_g_mmm_karma_severity );
91                 GameRules_scoring_add(attacker, MMM_KARMA, decreasekarma);
92                 attacker.karmapoints = attacker.karmapoints + decreasekarma;
93         }
94 }
95
96 void karmaWinDifference(entity it)
97 {
98         GameRules_scoring_add(it, SCORE, 1); // reward civilians who make it to the end of the round time limit
99         float increasekarma = ( autocvar_g_mmm_min_karma_points * random() * ( 0.1 + random() ) * 0.12 );
100         GameRules_scoring_add(it, MMM_KARMA, increasekarma);
101         it.karmapoints = it.karmapoints + increasekarma;
102         karma_Control(it);
103 }
104
105 void mmm_UpdateScores(bool timed_out)
106 {
107         // give players their hard-earned kills now that the round is over
108         FOREACH_CLIENT(true,
109         {
110                 it.totalfrags += it.mmm_validkills;
111                 if(it.mmm_validkills)
112                 {
113                         GameRules_scoring_add(it, SCORE, it.mmm_validkills);
114                 }
115                 it.mmm_validkills = 0;
116                 // player survived the round
117                 if(IS_PLAYER(it) && !IS_DEAD(it)) // LegendGuard adds something for Karma 21-02-2021
118                 {
119                         if((autocvar_g_mmm_reward_civilian && timed_out && it.mmm_status == MMM_STATUS_CIVILIAN) 
120                         || (autocvar_g_mmm_reward_civilian && !timed_out && it.mmm_status == MMM_STATUS_CIVILIAN))
121                         {
122                                 karmaWinDifference(it);
123                                 //PrintToChatAll(sprintf("^2REWARD ^7it.karmapoints: ^1%f", it.karmapoints));
124                         }
125
126                         //Sleuth reward after investigated a corpse
127                         if((autocvar_g_mmm_reward_sleuth && timed_out && it.mmm_status == MMM_STATUS_SLEUTH) 
128                         || (autocvar_g_mmm_reward_sleuth && !timed_out && it.mmm_status == MMM_STATUS_SLEUTH))
129                         {
130                                 if (it.investigated == true)
131                                 {
132                                         karmaWinDifference(it);
133                                         it.investigated = false;
134                                 }
135                         }
136
137                         if(it.mmm_status == MMM_STATUS_MURDERER)
138                         {
139                                 karmaWinDifference(it);
140                                 //PrintToChatAll(sprintf("^1MURDERER ^7it.karmapoints: ^1%f", it.karmapoints));
141                         }
142                 }
143         });
144 }
145
146 float mmm_CheckWinner()
147 {
148         if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
149         {
150                 // if the match times out, civilians win too!
151                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MMM_CIVILIAN_WIN);
152                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_MMM_CIVILIAN_WIN);
153                 FOREACH_CLIENT(true,
154                 {
155                         if(IS_PLAYER(it))
156                                 nades_Clear(it);
157                         mmm_FakeTimeLimit(it, -1);
158                         karma_Control(it);
159                 });
160
161                 mmm_UpdateScores(true);
162
163                 allowed_to_spawn = false;
164                 game_stopped = true;
165                 round_handler_Init(5, autocvar_g_mmm_warmup, autocvar_g_mmm_round_timelimit);
166                 return 1;
167         }
168
169         int civilian_count = 0, murderer_count = 0, sleuth_count = 0;
170         FOREACH_CLIENT(IS_PLAYER(it) && !IS_DEAD(it),
171         {
172                 if(it.mmm_status == MMM_STATUS_CIVILIAN)
173                         civilian_count++;
174                 else if(it.mmm_status == MMM_STATUS_MURDERER)
175                         murderer_count++;
176                 else if(it.mmm_status == MMM_STATUS_SLEUTH) //LegendGuard adds sleuth_count 20-02-2021 
177                         sleuth_count++;
178         });
179         if(civilian_count > 0 && murderer_count > 0)
180         {
181                 return 0;
182         }
183
184         if(murderer_count > 0) // murderers win
185         {
186                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MMM_MURDERER_WIN);
187                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_MMM_MURDERER_WIN);
188         }
189         else if(civilian_count > 0) // civilians win
190         {
191                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MMM_CIVILIAN_WIN);
192                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_MMM_CIVILIAN_WIN);
193         }
194         else if (sleuth_count > 0 && civilian_count > 0) // sleuths are same as civilians win
195         {
196                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MMM_CIVILIAN_WIN);
197                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_MMM_CIVILIAN_WIN);
198         }
199         else
200         {
201                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
202                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
203         }
204
205         mmm_UpdateScores(false);
206
207         allowed_to_spawn = false;
208         game_stopped = true;
209         round_handler_Init(5, autocvar_g_mmm_warmup, autocvar_g_mmm_round_timelimit);
210
211         FOREACH_CLIENT(true,
212         {
213                 if(IS_PLAYER(it))
214                 {
215                         it.respawn_flags = RESPAWN_SILENT; //CSQC print output respawn lib.qh error fix
216                         nades_Clear(it);
217                 }
218                 mmm_FakeTimeLimit(it, -1);
219                 karma_Control(it);
220         });
221
222         return 1;
223 }
224
225 void mmm_RoundStart()
226 {
227         allowed_to_spawn = boolean(warmup_stage);
228         int playercount = 0;
229
230         FOREACH_CLIENT(true,
231         {
232                 if(IS_PLAYER(it) && !IS_DEAD(it))
233                 {
234                         ++playercount;
235                         it.mmm_status = MMM_STATUS_CIVILIAN;
236                 }
237                 else
238                         it.mmm_status = 0; // this is mostly a safety check; if a client manages to somehow maintain a mmm status, clear it before the round starts!
239                 it.mmm_validkills = 0;
240         });
241         
242         int civilian_count = bound(1, ((autocvar_g_mmm_civilian_count >= 1) ? autocvar_g_mmm_civilian_count : floor(playercount * autocvar_g_mmm_civilian_count)), playercount - 1); // 20%, but ensure at least 1 and less than total
243         int total_civilians = 0;
244         //int murderer_count = bound(1, ((autocvar_g_mmm_murderer_count >= 1) ? autocvar_g_mmm_murderer_count : floor(playercount * autocvar_g_mmm_murderer_count)), playercount - 1); // 20%, but ensure at least 1 and less than total
245         int total_murderers = 0;
246         //int sleuth_count = bound(1, ((autocvar_g_mmm_sleuth_count >= 1) ? autocvar_g_mmm_sleuth_count : floor(playercount * autocvar_g_mmm_sleuth_count)), playercount - 1); // 20%, but ensure at least 1 and less than total
247         int total_sleuths = 0;
248
249         //civilians TOTAL
250         FOREACH_CLIENT_RANDOM(IS_PLAYER(it) && !IS_DEAD(it),
251         {
252                 if(total_civilians >= civilian_count)
253                         break;
254                 //LegendGuard fixes the round start again 22-03-2021
255                 total_civilians++;
256                 if (total_civilians <= 1)
257                 {
258                         if (total_murderers <= 1)
259                         {
260                                 total_murderers++;
261                                 it.mmm_status = MMM_STATUS_MURDERER;
262                         }
263                 }
264                 else if (total_civilians == 2)
265                 {
266                         if (total_sleuths >= 1)
267                                 break;
268                         else
269                         {
270                                 total_sleuths++;
271                                 it.mmm_status = MMM_STATUS_SLEUTH;
272                         }
273                 }
274                 else if (total_civilians == 5)
275                 {
276                         if (total_murderers <= 2)
277                                 break;
278                         else
279                         {
280                                 total_murderers++;
281                                 it.mmm_status = MMM_STATUS_MURDERER;
282                         }
283                 }
284                 else if (total_civilians == 6)
285                 {
286                         if (total_sleuths >= 2)
287                                 break;
288                         else
289                         {
290                                 total_sleuths++;
291                                 it.mmm_status = MMM_STATUS_SLEUTH;
292                         }
293                 }
294                 else if (total_civilians == 7)
295                 {
296                         if (total_sleuths >= 3)
297                                 break;
298                         else if (total_murderers == 3)
299                         {
300                                 total_murderers++;
301                                 it.mmm_status = MMM_STATUS_MURDERER;
302                         }
303                         else
304                         {
305                                 total_sleuths++;
306                                 it.mmm_status = MMM_STATUS_SLEUTH;
307                         }
308                 }
309                 else if (total_civilians >= 8)
310                 {
311                         if (total_sleuths >= 4)
312                                 break;
313                         else if (total_murderers == 4)
314                         {
315                                 total_murderers++;
316                                 it.mmm_status = MMM_STATUS_MURDERER;
317                         }
318                         else
319                         {
320                                 total_sleuths++;
321                                 it.mmm_status = MMM_STATUS_SLEUTH;
322                         }
323                 }
324                 else
325                 {
326                         total_murderers++; 
327                         it.mmm_status = MMM_STATUS_MURDERER;
328                 }
329         });
330
331         FOREACH_CLIENT(IS_PLAYER(it) && !IS_DEAD(it),
332         {
333                 float totalmeankarma = ((autocvar_g_mmm_max_karma_points + autocvar_g_mmm_min_karma_points + it.karmapoints) / 3);
334                 karma_Control(it);
335                 it.activekillerrole = false;
336
337                 if(it.mmm_status == MMM_STATUS_CIVILIAN)
338                 {
339                         SetResource(it, RES_BULLETS, autocvar_g_mmm_playerammobrcstartquantity);
340                         SetResource(it, RES_ROCKETS, autocvar_g_mmm_playerammobrcstartquantity);
341                         SetResource(it, RES_CELLS, autocvar_g_mmm_playerammobrcstartquantity);
342                         if (it.karmapoints <= totalmeankarma)
343                         {
344                                 centerprint(it, strcat(BOLD_OPERATOR, "^1KARMA WARNING!\n^3Here, have the Rifle!"));
345                                 GiveWeapon(it, WEP_RIFLE.m_id, OP_PLUS, 1);
346                         }
347                         //Gives Mine Layer weapon to the player
348                         GiveWeapon(it, WEP_MINE_LAYER.m_id, OP_PLUS, 1);
349                         Send_Notification(NOTIF_ONE_ONLY, it, MSG_CENTER, CENTER_MMM_CIVILIAN);
350                         Send_Notification(NOTIF_ONE_ONLY, it, MSG_INFO, INFO_MMM_CIVILIAN);
351                         //PrintToChatAll(sprintf("^1DEBUG^7: %s is ^2Civilian^7!", it.netname));
352                 }
353                 else if(it.mmm_status == MMM_STATUS_MURDERER)
354                 {
355                         SetResource(it, RES_BULLETS, autocvar_g_mmm_playerammobrcstartquantity);
356                         SetResource(it, RES_ROCKETS, autocvar_g_mmm_playerammobrcstartquantity);
357                         SetResource(it, RES_CELLS, autocvar_g_mmm_playerammobrcstartquantity);
358                         if (it.karmapoints <= totalmeankarma)
359                         {
360                                 centerprint(it, strcat(BOLD_OPERATOR, "^1KARMA WARNING!\n^3Here, have the Rifle!"));
361                                 GiveWeapon(it, WEP_RIFLE.m_id, OP_PLUS, 1);
362                         }
363                         //Gives Mine Layer weapon to the player
364                         GiveWeapon(it, WEP_MINE_LAYER.m_id, OP_PLUS, 1);
365                         Send_Notification(NOTIF_ONE_ONLY, it, MSG_CENTER, CENTER_MMM_MURDERER);
366                         Send_Notification(NOTIF_ONE_ONLY, it, MSG_INFO, INFO_MMM_MURDERER);
367                         //PrintToChatAll(sprintf("^1DEBUG^7: %s is ^1Murderer^7!", it.netname));
368                 }
369                 else if(it.mmm_status == MMM_STATUS_SLEUTH)
370                 {
371                         SetResource(it, RES_ROCKETS, 10);
372                         if (it.karmapoints <= totalmeankarma)
373                         {
374                                 centerprint(it, strcat(BOLD_OPERATOR, "^1KARMA WARNING!\n^3Here, have the Rifle!"));
375                                 GiveWeapon(it, WEP_RIFLE.m_id, OP_PLUS, 1);
376                         }
377                         //Gives Shockwave and Mine Layer weapon to the player
378                         GiveWeapon(it, WEP_SHOCKWAVE.m_id, OP_PLUS, 1);
379                         GiveWeapon(it, WEP_MINE_LAYER.m_id, OP_PLUS, 1);
380                         Send_Notification(NOTIF_ONE_ONLY, it, MSG_CENTER, CENTER_MMM_SLEUTH);
381                         Send_Notification(NOTIF_ONE_ONLY, it, MSG_INFO, INFO_MMM_SLEUTH);
382                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_MMM_WHOISSLEUTH, it.netname);
383                 }
384                 mmm_FakeTimeLimit(it, round_handler_GetEndTime());
385         });
386 }
387
388 bool mmm_CheckPlayers()
389 {
390         static int prev_missing_players;
391         allowed_to_spawn = true;
392         int playercount = 0;
393
394         FOREACH_CLIENT(IS_PLAYER(it) && !IS_DEAD(it),
395         {       
396                 //PrintToChatAll(sprintf("it.karmapoints ^5begin: ^3%f",it.karmapoints));
397                 //Karma points start
398                 if (it.karmastarted != true)
399                 {
400                         GameRules_scoring_add(it, MMM_KARMA, autocvar_g_mmm_max_karma_points - it.karmapoints);
401                         it.karmapoints = autocvar_g_mmm_max_karma_points;
402                         it.karmastarted = true;
403                 }
404                 karma_Control(it);
405                 ++playercount;
406                 //PrintToChatAll(sprintf("it.karmapoints ^6end: ^3%f",it.karmapoints));
407         });
408
409         if (playercount >= 2)
410         {
411                 if(prev_missing_players > 0)
412                         Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_PLAYERS);
413                 prev_missing_players = -1;
414                 return true;
415         }
416
417         if(playercount == 0)
418         {
419                 if(prev_missing_players > 0)
420                         Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_PLAYERS);
421                 prev_missing_players = -1;
422                 return false;
423         }
424
425         // if we get here, only 1 player is missing
426         if(prev_missing_players != 1)
427         {
428                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_PLAYERS, 1);
429                 prev_missing_players = 1;
430         }
431         return false;
432 }
433
434 bool mmm_isEliminated(entity e)
435 {
436         if(e.caplayer == 1 && (IS_DEAD(e) || e.frags == FRAGS_PLAYER_OUT_OF_GAME))
437                 return true;
438         if(e.caplayer == 0.5)
439                 return true;
440         return false;
441 }
442
443 void mmm_Initialize() // run at the start of a match, initiates game mode
444 {
445         GameRules_scoring(0, SFL_SORT_PRIO_PRIMARY, 0, {
446                 field(SP_MMM_KARMA, "karma", SFL_SORT_PRIO_SECONDARY); //LegendGuard adds Karma points in the scoreboard 22-02-2021
447         });
448
449         allowed_to_spawn = true;
450         round_handler_Spawn(mmm_CheckPlayers, mmm_CheckWinner, mmm_RoundStart);
451         round_handler_Init(5, autocvar_g_mmm_warmup, autocvar_g_mmm_round_timelimit);
452         EliminatedPlayers_Init(mmm_isEliminated);
453 }
454
455 void checkWeaponDeathtype(entity target, float deathtype)
456 {
457         switch (deathtype)
458         {
459                 case WEP_ARC.m_id: case 276: case 788: target.killedwithweapon = "Impacted by the Arc's electric shock"; return;
460                 case WEP_BLASTER.m_id: case 513: target.killedwithweapon = "Blasted by the Blaster"; return;
461                 case WEP_CRYLINK.m_id: case 263: case 519: target.killedwithweapon = "Shot by the Crylink"; return;
462                 case WEP_DEVASTATOR.m_id: case 522: case 1546: target.killedwithweapon = "Bombarded by the Devastator"; return;
463                 case WEP_ELECTRO.m_id: case 262: case 518: case 1542: target.killedwithweapon = "Electrocuted by the Electro"; return;
464                 case WEP_FIREBALL.m_id: case 273: case 529: case 1297: target.killedwithweapon = "Burned by the Fireball"; return;
465                 case WEP_HAGAR.m_id: case 265: target.killedwithweapon = "Gunned by the Hagar"; return;
466                 case WEP_HLAC.m_id: case 270: case 526: target.killedwithweapon = "Cut down with the HLAC"; return;
467                 case WEP_HOOK.m_id: case 1805: target.killedwithweapon = "Caught in Hook gravity bomb"; return;
468                 case WEP_MACHINEGUN.m_id: target.activekillerrole = true; target.killedwithweapon = "Riddled full of holes by the Machine Gun"; return;
469                 case WEP_MINE_LAYER.m_id: case 517: case 1541: target.killedwithweapon = "Exploited by the Mine Layer"; return;
470                 case WEP_MORTAR.m_id: case 516: case 1284: target.killedwithweapon = "Blew up with the Mortar"; return;
471                 case WEP_OVERKILL_NEX.m_id: target.killedwithweapon = "Sniped by the Overkill Nex"; return;
472                 case WEP_RIFLE.m_id: case 272: target.activekillerrole = true; target.killedwithweapon = "Sniped by the Rifle"; return;
473                 case WEP_SEEKER.m_id: case 274: case 786: target.killedwithweapon = "Blasted by the Seeker"; return;
474                 case WEP_SHOCKWAVE.m_id: target.killedwithweapon = "Gunned down by the Shockwave"; return;
475                 case 275: target.killedwithweapon = "Knocked by the Shockwave"; return;
476                 case WEP_SHOTGUN.m_id: target.activekillerrole = true; target.killedwithweapon = "Shot by Shotgun"; return;
477                 case 258: target.killedwithweapon = "Knocked by the Shotgun"; return;
478                 case WEP_TUBA.m_id: target.killedwithweapon = "Ear pain by the @!#%'n Tuba"; return;
479                 case WEP_VAPORIZER.m_id: case 257: case 769: target.killedwithweapon = "Sniped by the Vaporizer"; return;
480                 case WEP_VORTEX.m_id: target.killedwithweapon = "Sniped by the Vortex"; return;
481                 case DEATH_FALL.m_id: target.killedwithweapon = "Fall"; return;
482                 case DEATH_FIRE.m_id: target.killedwithweapon = "Burned with the fire"; return;
483                 case DEATH_LAVA.m_id: target.killedwithweapon = "Burned in lava"; return;
484                 case DEATH_MIRRORDAMAGE.m_id: target.killedwithweapon = "Suicide"; return;
485                 case DEATH_SLIME.m_id: target.killedwithweapon = "Melted in slime"; return;
486                 case DEATH_TELEFRAG.m_id: target.killedwithweapon = "Telefragged"; return;
487                 case DEATH_NADE.m_id: target.killedwithweapon = "Blown up by the nade"; return;
488                 case DEATH_NADE_NAPALM.m_id: target.killedwithweapon = "Burned by the Napalm nade"; return;
489                 case DEATH_NADE_ICE.m_id: target.killedwithweapon = "Frozen by the Ice nade"; return;
490                 case DEATH_NADE_HEAL.m_id: target.killedwithweapon = "Sucked by the Heal nade"; return;
491                 default: target.killedwithweapon = "Unknown"; return;
492         }
493 }
494
495 void ReduceKarmaPointsandFrags(entity frag_attacker, entity frag_target, float frag_deathtype, entity wep_ent)
496 {
497         karmaLoseDifference(frag_attacker, frag_target);
498         GiveFrags(frag_attacker, frag_target, ((autocvar_g_mmm_punish_teamkill) ? -1 : -2), frag_deathtype, wep_ent.weaponentity_fld);
499         frag_target.whokilled = frag_attacker.netname;
500 }
501
502 // ==============
503 // Hook Functions
504 // ==============
505
506 MUTATOR_HOOKFUNCTION(mmm, ClientObituary)
507 {
508         // LegendGuard's IDEA: To adjust the grade of severity of karma, 
509         // we could add if sentence per weapons and adjust each weapon attack
510         // its own grade. Instead doing random decrease grade 22-02-2021
511         
512         // in mmm, announcing a frag would tell everyone who the murderer is
513         entity frag_attacker = M_ARGV(1, entity);
514         entity frag_target = M_ARGV(2, entity);
515         
516         if(IS_PLAYER(frag_attacker) && frag_attacker != frag_target)
517         {
518                 float frag_deathtype = M_ARGV(3, float);
519                 entity wep_ent = M_ARGV(4, entity);
520                 
521                 //PrintToChatAll(strcat("deathtype var: ", ftos(frag_deathtype)));
522                 checkWeaponDeathtype(frag_target, frag_deathtype);
523                 // "team" kill, a point is awarded to the player by default so we must take it away plus an extra one
524                 // unless the player is going to be punished for suicide, in which case just remove one
525                 if(frag_attacker.mmm_status == frag_target.mmm_status)
526                 {
527                         //PrintToChatAll("^1DEBUG^7: A ^2PLAYER^7 has fragged a ^2PLAYER OF HIS OWN TEAM^7, TOO BAD!");
528                         ReduceKarmaPointsandFrags(frag_attacker, frag_target, frag_deathtype, wep_ent);
529                         switch (frag_attacker.mmm_status)
530                         {
531                                 case MMM_STATUS_CIVILIAN: frag_target.killerrole = "\n^3Killer role: ^2Civilian"; return;
532                                 case MMM_STATUS_MURDERER: frag_target.killerrole = "\n^3Killer role: ^1Murderer"; return;
533                                 case MMM_STATUS_SLEUTH: frag_target.killerrole = "\n^3Killer role: ^4Sleuth"; return;
534                                 default: return;
535                         }
536                         //PrintToChatAll(sprintf("frag_attacker.karmapoints: ^1%f", frag_attacker.karmapoints));
537                 }
538
539                 if(frag_attacker.mmm_status == MMM_STATUS_SLEUTH)
540                 {
541                         if (frag_target.mmm_status == MMM_STATUS_CIVILIAN || frag_target.mmm_status == MMM_STATUS_SLEUTH)
542                         {       
543                                 //PrintToChatAll("^1DEBUG^7: A ^4Sleuth^7 fragged an ^2Civilian^7/^4Sleuth^7, TOO BAD!");
544                                 ReduceKarmaPointsandFrags(frag_attacker, frag_target, frag_deathtype, wep_ent);
545                                 frag_target.killerrole = "\n^3Killer role: ^4Sleuth";
546                                 //PrintToChatAll(sprintf("frag_attacker.karmapoints: ^1%f", frag_attacker.karmapoints));
547                         }
548                         else
549                         {
550                                 frag_target.whokilled = frag_attacker.netname;
551                                 frag_target.killerrole = "\n^3Killer role: ^4Sleuth";
552                         }
553                 }
554
555                 if (frag_attacker.mmm_status == MMM_STATUS_CIVILIAN)
556                 {
557                         if (frag_target.mmm_status == MMM_STATUS_SLEUTH)
558                         {
559                                 //PrintToChatAll("^1DEBUG^7: An ^2Civilian^7 fragged a ^4Sleuth^7, TOO BAD!");
560                                 ReduceKarmaPointsandFrags(frag_attacker, frag_target, frag_deathtype, wep_ent);
561                                 frag_target.killerrole = "\n^3Killer role: ^2Civilian";
562                         }
563                         else
564                         {
565                                 frag_target.whokilled = frag_attacker.netname;
566                                 frag_target.killerrole = "\n^3Killer role: ^2Civilian";
567                         }
568                 }
569                 
570                 if (frag_attacker.mmm_status == MMM_STATUS_MURDERER)
571                 {
572                         if (frag_target.mmm_status == MMM_STATUS_CIVILIAN)
573                         {
574                                 frag_target.whokilled = frag_attacker.netname;
575                                 frag_target.killerrole = "\n^3Killer role: ^1Murderer";
576                         }
577                         else
578                         {
579                                 frag_target.whokilled = frag_attacker.netname;
580                                 frag_target.killerrole = "\n^3Killer role: ^1Murderer";
581                         }
582                 }
583                 //if mmm_status is 1, means civilian, 2 means murderer, 3 means sleuth
584                 //PrintToChatAll(sprintf("^1DEBUG^7: frag_attacker.mmm_status is ^3%s^7",ftos(frag_attacker.mmm_status)));
585                 //PrintToChatAll(sprintf("^1DEBUG^7: frag_target.mmm_status is ^3%s^7",ftos(frag_target.mmm_status)));
586         }
587         else
588         {
589                 float frag_deathtype = M_ARGV(3, float);
590                 checkWeaponDeathtype(frag_target, frag_deathtype);
591         }
592
593         M_ARGV(5, bool) = true; // anonymous attacker
594 }
595
596 //karma weapon damage, halve the damage attack when player has low karma 20-03-2021
597 MUTATOR_HOOKFUNCTION(mmm, Damage_Calculate)
598 {
599         entity attacker = M_ARGV(1, entity);
600         entity target = M_ARGV(2, entity);
601         float deathtype = M_ARGV(3, float);
602         float damage = M_ARGV(4, float);
603         vector force = M_ARGV(6, vector);
604         string corpsemessagestrcat = "";
605
606         if (autocvar_g_mmm_karma_damageactive != false)
607         {
608                 if (IS_PLAYER(attacker))
609                 {
610                         if(target == attacker) // damage done to yourself
611                         {
612                                 damage /= autocvar_g_weapondamagefactor / (attacker.karmapoints / autocvar_g_mmm_max_karma_points);
613                                 force /= autocvar_g_weaponforcefactor / (attacker.karmapoints / autocvar_g_mmm_max_karma_points);
614                         }
615                         else if (target != attacker)
616                         {
617                                 damage /= autocvar_g_weapondamagefactor / (attacker.karmapoints / autocvar_g_mmm_max_karma_points);
618                                 force /= autocvar_g_weaponforcefactor / (attacker.karmapoints / autocvar_g_mmm_max_karma_points);
619                         }
620                         else
621                         {
622                                 damage *= autocvar_g_weapondamagefactor;
623                                 force *= autocvar_g_weaponforcefactor;
624                         }
625                 }
626         }
627
628         //CORPSE DETECTION SKILL 21-03-2021
629         if(IS_DEAD(target))
630         {
631                 //Shockwave weapon as radar gun to check the corpses 22-03-2021
632                 if(DEATH_ISWEAPON(deathtype, WEP_SHOCKWAVE))
633                 {
634                         if (target.killedwithweapon == "")
635                                 target.killedwithweapon = "UNKNOWN CAUSE";
636                         
637                         if (target.activekillerrole != true)
638                         {
639                                 target.killerrole = "";
640                                 target.activekillerrole = false;
641                         }
642
643                         string killedbyphrase = strcat("\n^3Killed by:^7 ", target.whokilled, target.killerrole); 
644                         string wepkilledphrase = strcat("\n^3Cause:^7 ", target.killedwithweapon);
645                         if (target.whokilled == "")
646                         {
647                                 killedbyphrase = "";
648                                 if (target.killedwithweapon == "")
649                                         wepkilledphrase = "\n^3Cause:^7 UNKNOWN CAUSE";
650                         }
651
652                         damage = 0;
653                         force = '0 0 0';
654                         if (target.mmm_status == MMM_STATUS_CIVILIAN)
655                         {
656                                 //try to add centerprint message for chat privately if possible
657                                 corpsemessagestrcat = strcat("\n^3Name:^7 ", target.netname, "\n^3Role: ^2Civilian", killedbyphrase, wepkilledphrase);
658                                 centerprint(attacker, strcat(BOLD_OPERATOR, corpsemessagestrcat));//("\n^6Name^3:^7 ", target.netname, "\n^5Role^3: ^2Civilian\n", "^1Killed by^3:^7 ", target.whokilled)));
659                         }
660                         else if (target.mmm_status == MMM_STATUS_MURDERER)
661                         {
662                                 corpsemessagestrcat = strcat("\n^3Name:^7 ", target.netname, "\n^3Role: ^1Murderer", killedbyphrase, wepkilledphrase);
663                                 centerprint(attacker, strcat(BOLD_OPERATOR, corpsemessagestrcat));//("\n^6Name^3:^7 ", target.netname, "\n^5Role^3: ^1Murderer\n", "^1Killed by^3:^7 ", target.whokilled)));
664                         }
665                         else if (target.mmm_status == MMM_STATUS_SLEUTH)
666                         {
667                                 corpsemessagestrcat = strcat("\n^3Name:^7 ", target.netname, "\n^3Role: ^4Sleuth", killedbyphrase, wepkilledphrase);
668                                 centerprint(attacker, strcat(BOLD_OPERATOR, corpsemessagestrcat));//("\n^6Name^3:^7 ", target.netname, "\n^5Role^3: ^4Sleuth\n", "^1Killed by^3:^7 ", target.whokilled)));
669                         }
670                         attacker.investigated = true;
671                 }
672         }
673
674         M_ARGV(4, float) = damage;
675         M_ARGV(6, vector) = force;
676 }
677
678 MUTATOR_HOOKFUNCTION(mmm, PlayerPreThink)
679 {
680         entity player = M_ARGV(0, entity);
681         
682         if(IS_PLAYER(player) || player.caplayer)
683         {
684                 // update the scoreboard colour display to out the real killer at the end of the round
685                 // running this every frame to avoid cheats
686                 int plcolor = MMM_COLOR_CIVILIAN;
687                 if(player.mmm_status == MMM_STATUS_CIVILIAN && game_stopped) //Civilian status by default
688                         plcolor = MMM_COLOR_CIVILIAN;
689                 if(player.mmm_status == MMM_STATUS_MURDERER && game_stopped)
690                         plcolor = MMM_COLOR_MURDERER;
691                 //LegendGuard adds for Sleuth 21-02-2021
692                 if(player.mmm_status == MMM_STATUS_SLEUTH)// && game_stopped)
693                         plcolor = MMM_COLOR_SLEUTH;
694                 setcolor(player, plcolor);
695         }
696
697         //CORPSE FEATURE 10-03-2021
698         if (IS_DEAD(player))
699         {
700                 player.event_damage = func_null;
701                 //player.health = 0;
702                 player.solid = SOLID_CORPSE;
703                 set_movetype(player, MOVETYPE_STEP); //test with MOVETYPE_TOSS or MOVETYPE_WALK (it's like sliding object) or MOVETYPE_BOUNCE (maybe not good)
704         }
705 }
706
707 MUTATOR_HOOKFUNCTION(mmm, PlayerSpawn)
708 {
709         entity player = M_ARGV(0, entity);
710
711         player.mmm_status = 0;
712         player.mmm_validkills = 0;
713         player.caplayer = 1;
714         if (!warmup_stage)
715                 eliminatedPlayers.SendFlags |= 1;
716 }
717
718 MUTATOR_HOOKFUNCTION(mmm, ForbidSpawn)
719 {
720         entity player = M_ARGV(0, entity);
721
722         // spectators / observers that weren't playing can join; they are
723         // immediately forced to observe in the PutClientInServer hook
724         // this way they are put in a team and can play in the next round
725         if (!allowed_to_spawn && player.caplayer)
726                 return true;
727         return false;
728 }
729
730 MUTATOR_HOOKFUNCTION(mmm, PutClientInServer)
731 {
732         entity player = M_ARGV(0, entity);
733
734         if (!allowed_to_spawn && IS_PLAYER(player)) // this is true even when player is trying to join
735         {
736                 TRANSMUTE(Observer, player);
737                 if (CS(player).jointime != time && !player.caplayer) // not when connecting
738                 {
739                         player.caplayer = 0.5;
740                         Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_JOIN_LATE);
741                 }
742         }
743 }
744
745 MUTATOR_HOOKFUNCTION(mmm, reset_map_players)
746 {
747         FOREACH_CLIENT(true, {
748                 CS(it).killcount = 0;
749                 it.mmm_status = 0;
750                 mmm_FakeTimeLimit(it, -1); // restore original timelimit
751                 if (!it.caplayer && IS_BOT_CLIENT(it))
752                         it.caplayer = 1;
753                 if (it.caplayer)
754                 {
755                         TRANSMUTE(Player, it);
756                         it.caplayer = 1;
757                         it.respawn_flags = RESPAWN_SILENT; //CSQC print output respawn lib.qh error fix
758                         PutClientInServer(it);
759                 }
760         });
761         bot_relinkplayerlist();
762         return true;
763 }
764
765 MUTATOR_HOOKFUNCTION(mmm, reset_map_global)
766 {
767         allowed_to_spawn = true;
768         return true;
769 }
770
771 entity mmm_LastPlayerForTeam(entity this)
772 {
773         entity last_pl = NULL;
774         FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
775                 if (!IS_DEAD(it) && this.mmm_status == it.mmm_status)
776                 {
777                         if (!last_pl)
778                         {
779                                 last_pl = it;
780                         }
781                         else
782                                 return NULL;
783                 }
784         });
785         return last_pl;
786 }
787
788 void mmm_LastPlayerForTeam_Notify(entity this)
789 {
790         if (!warmup_stage && round_handler_IsActive() && round_handler_IsRoundStarted())
791         {
792                 entity pl = mmm_LastPlayerForTeam(this);
793                 if (pl)
794                         Send_Notification(NOTIF_ONE_ONLY, pl, MSG_CENTER, CENTER_ALONE);
795         }
796 }
797
798 MUTATOR_HOOKFUNCTION(mmm, PlayerDies)
799 {
800         entity frag_attacker = M_ARGV(1, entity);
801         entity frag_target = M_ARGV(2, entity);
802         //float frag_deathtype = M_ARGV(3, float);
803
804         mmm_LastPlayerForTeam_Notify(frag_target);
805         if (!allowed_to_spawn)
806         {
807                 frag_target.respawn_flags = RESPAWN_DENY;
808                 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
809                 frag_target.respawn_time = time + 2;
810         }
811         frag_target.respawn_flags |= RESPAWN_DENY;
812         frag_target.event_damage = func_null;
813         frag_target.health = 0;
814         
815         if (!warmup_stage)
816         {
817                 eliminatedPlayers.SendFlags |= 1;
818                 if (IS_BOT_CLIENT(frag_target))
819                         bot_clear(frag_target);
820         }
821         
822         //if(frag_attacker.mmm_status == frag_target.mmm_status)
823         // killed an ally! punishment is sentenced
824         if(frag_attacker.mmm_status == MMM_STATUS_SLEUTH)
825         {
826                 if (frag_target.mmm_status == MMM_STATUS_CIVILIAN)
827                 {
828                         //PrintToChatAll("^1DEBUG^7: ^4SLEUTH ^1DAMAGE/DEAD^7 HAS TAKEN!");
829                         Damage(frag_attacker, frag_attacker, frag_attacker, autocvar_g_mmm_karma_damagepunishmentdeal, DEATH_MIRRORDAMAGE.m_id, DMG_NOWEP, frag_attacker.origin, '0 0 0');
830                 }
831         }
832         if (frag_attacker.mmm_status == MMM_STATUS_CIVILIAN)
833         {
834                 if (frag_target.mmm_status == MMM_STATUS_SLEUTH)
835                 {
836                         //PrintToChatAll("^1DEBUG^7: ^2CIVILIAN ^1DAMAGE/DEAD^7 HAS TAKEN!");
837                         Damage(frag_attacker, frag_attacker, frag_attacker, autocvar_g_mmm_karma_damagepunishmentdeal, DEATH_MIRRORDAMAGE.m_id, DMG_NOWEP, frag_attacker.origin, '0 0 0');
838                 }
839         }
840         if (frag_attacker.mmm_status == MMM_STATUS_MURDERER)
841         {
842                 if (frag_target.mmm_status == MMM_STATUS_MURDERER)
843                 {
844                         //PrintToChatAll("^1DEBUG^7: ^1MURDERER ^1DAMAGE/DEAD^7 HAS TAKEN!");
845                         Damage(frag_attacker, frag_attacker, frag_attacker, autocvar_g_mmm_karma_damagepunishmentdeal, DEATH_MIRRORDAMAGE.m_id, DMG_NOWEP, frag_attacker.origin, '0 0 0');
846                 }
847         }
848         return true;
849 }
850
851 MUTATOR_HOOKFUNCTION(mmm, ClientDisconnect)
852 {
853         entity player = M_ARGV(0, entity);
854
855         if (IS_PLAYER(player) && !IS_DEAD(player))
856                 mmm_LastPlayerForTeam_Notify(player);
857         return true;
858 }
859
860 MUTATOR_HOOKFUNCTION(mmm, MakePlayerObserver)
861 {
862         entity player = M_ARGV(0, entity);
863
864         if (IS_PLAYER(player) && !IS_DEAD(player))
865                 mmm_LastPlayerForTeam_Notify(player);
866         if (player.killindicator_teamchange == -2) // player wants to spectate
867                 player.caplayer = 0;
868         if (player.caplayer)
869                 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
870         if (!warmup_stage)
871                 eliminatedPlayers.SendFlags |= 1;
872         if (!player.caplayer)
873         {
874                 player.mmm_validkills = 0;
875                 player.mmm_status = 0;
876                 mmm_FakeTimeLimit(player, -1); // restore original timelimit
877                 return false;  // allow team reset
878         }
879         return true;  // prevent team reset
880 }
881
882 MUTATOR_HOOKFUNCTION(mmm, Scores_CountFragsRemaining)
883 {
884         // announce remaining frags?
885         return true;
886 }
887
888 MUTATOR_HOOKFUNCTION(mmm, GiveFragsForKill, CBC_ORDER_FIRST)
889 {
890         entity frag_attacker = M_ARGV(0, entity);
891         if(!warmup_stage && round_handler_IsActive() && round_handler_IsRoundStarted()) 
892                 frag_attacker.mmm_validkills += M_ARGV(2, float);
893         M_ARGV(2, float) = 0; // score will be given to the winner when the round ends
894         return true;
895 }
896
897 MUTATOR_HOOKFUNCTION(mmm, AddPlayerScore)
898 {
899         // add scorefield for scoreboard here
900         entity scorefield = M_ARGV(0, entity);
901         if(scorefield == SP_KILLS || scorefield == SP_DEATHS || scorefield == SP_SUICIDES || scorefield == SP_DMG || scorefield == SP_DMGTAKEN)
902                 M_ARGV(1, float) = 0; // don't report that the player has killed or been killed, that would out them as a murderer!
903 }
904
905 MUTATOR_HOOKFUNCTION(mmm, CalculateRespawnTime)
906 {
907         // no respawn calculations needed, player is forced to spectate anyway
908         return true;
909 }
910
911 MUTATOR_HOOKFUNCTION(mmm, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
912 {
913         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
914                 if (IS_PLAYER(it) || it.caplayer == 1)
915                         ++M_ARGV(0, int);
916                 ++M_ARGV(1, int);
917         });
918         return true;
919 }
920
921 MUTATOR_HOOKFUNCTION(mmm, ClientCommand_Spectate)
922 {
923         entity player = M_ARGV(0, entity);
924
925         if (player.caplayer)
926         {
927                 // they're going to spec, we can do other checks
928                 if (autocvar_sv_spectate && (IS_SPEC(player) || IS_OBSERVER(player)))
929                         Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_LEAVE);
930                 return MUT_SPECCMD_FORCE;
931         }
932
933         return MUT_SPECCMD_CONTINUE;
934 }
935
936 MUTATOR_HOOKFUNCTION(mmm, GetPlayerStatus)
937 {
938         entity player = M_ARGV(0, entity);
939
940         return player.caplayer == 1;
941 }
942
943 MUTATOR_HOOKFUNCTION(mmm, BotShouldAttack)
944 {
945         entity bot = M_ARGV(0, entity);
946         entity targ = M_ARGV(1, entity);
947
948         if(targ.mmm_status == bot.mmm_status)
949         {
950                 return true;
951         }
952         
953         // LegendGuard fixed the problem of Sleuths and Civilians attacking each other 26-03-2021
954         if(bot.mmm_status == MMM_STATUS_SLEUTH)
955         {
956                 if(targ.mmm_status == MMM_STATUS_CIVILIAN)
957                         return true;
958         }
959 }