Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
sv_mayhem.qc
Go to the documentation of this file.
1#include "sv_mayhem.qh"
2#include <common/scores.qh>
3
4// import autocvars for MayhemCalculatePlayerScore's teamplay cvars
6
9
22
30
32
33/*
34// unused for now
35void mayhem_DelayedInit(entity this)
36{
37 return;
38}
39*/
40
42{
45
46 // unused for now
47 //InitializeEntity(NULL, mayhem_DelayedInit, INITPRIO_GAMETYPE);
48}
49
50/*
51MUTATOR_HOOKFUNCTION(mayhem, Scores_CountFragsRemaining)
52{
53 // do not announce remaining frags, upscaled score count doesn't match well with this
54 // when scorelimit is set to 1000 it would announce 997, 998 and 999 score counts
55 // usually a single shot which deals ~40-80 dmg gives 2 or 3 score
56 // this usually would cause a "2 fra..." announcement to be played as the match ends
57 // without leaving anyone time to even process the announcement
58 return false;
59}
60*/
61
76
78{
80 M_ARGV(2, float) = 0;
82 M_ARGV(3, float) = 0;
84}
85
86MUTATOR_HOOKFUNCTION(mayhem, ForbidThrowCurrentWeapon)
87{
88 return true;
89}
90
92{
93 if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
95}
96
98{
99 entity item = M_ARGV(0, entity);
100
101 // enable powerups if forced globally or global accepts gametypes to have powerups according to their own settings
103 if (item.itemdef.instanceOfPowerup){
104 return false;
105 }
106 }
107 // disabled powerups if forced off globally or in this gametype
109 if (item.itemdef.instanceOfPowerup){
110 return true;
111 }
112 }
113 // remove all items if items are forced off globally
114 if (autocvar_g_pickup_items == 0){
115 return true;
116 }
117 // if items are switched on in this gametype allow the removal of weapons and ammo still
119 if (item.itemdef.instanceOfAmmo || item.itemdef.instanceOfWeaponPickup){
120 return true;
121 }
122 }
123 // remove items if not globally set to follow mode's settings and locally set off
125 return true;
126 }
127 return false;
128}
129
130MUTATOR_HOOKFUNCTION(mayhem, Damage_Calculate)
131{
132 entity frag_attacker = M_ARGV(1, entity);
134 float frag_deathtype = M_ARGV(3, float);
135 float frag_damage = M_ARGV(4, float);
136
137 if (IS_PLAYER(frag_target)) // nullify self-damage if self-damage is disabled and always nullify splat
138 if (!IS_DEAD(frag_target)) // but enable anyone to gib corpses, even their own corpses with delayed damage
139 if ((autocvar_g_mayhem_selfdamage == 0 && frag_target == frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
140 frag_damage = 0;
141
142 M_ARGV(4, float) = frag_damage;
143}
144
146{
147 int scoringmethod = 1;
148 float upscaler; // how much score does 1 frag give
149 float frag_weight; // how many frags should a kill be worth
150 float damage_weight; // how many frags is damage worth of player's spawn health+armor
151
152 // if frag is 0.25 , damage is 0.75 and upscaler is 20
153 // then killing a full hp opponent is 1 frag which is worth of 20 score
154 // or damage worth of 2 starting health+armor is 1.5 frags or 30 score even if no kills were gotten
155
156 bool disable_selfdamage2score;
157
158 if (teamplay) {
159 // use Team Mayhem values
164 } else {
165 // use FFA Mayhem values
170 }
171
172 // decide scoringmethod and avoid potential divide by 0 errors
173
174 if (frag_weight && damage_weight) // both frag and damage weights have non-zero values
175 scoringmethod = 1;
176 else if (frag_weight) // frag weight has a value
177 scoringmethod = 2;
178 else if (damage_weight) // damage weight has a value
179 scoringmethod = 3;
180 else
181 return; // neither frags nor damage are set to give score
182
183 switch (scoringmethod)
184 {
185 default:
186 case 1:
187 {
188 // calculate how much score the player should have based on their damage dealt and frags gotten and then add the missing score
189
190 // give a different weight for suicides if scoring method 1 doesn't have selfdamage2score enabled to harshly punish for suicides to avoid exploiting
191 float suicide_weight = 1 + (disable_selfdamage2score / frag_weight);
192
193 // total damage divided by player start health&armor to get how many lives worth of damage they've dealt,
194 // then calculate new value affected by weight
195 float playerdamagescore = ((scorer.total_damage_dealt / (start_health + start_armorvalue)) * 100) * upscaler * damage_weight;
196 // * 100 to avoid float inaccuracy at that decimal level
197
198 // playerdamagescore rounded to one decimal
199 float roundedplayerdamagescore = rint(playerdamagescore * 10) / 10;
200
201 // amount of kills
202 float killcount = PlayerScore_Get(scorer, SP_KILLS) - PlayerScore_Get(scorer, SP_TEAMKILLS) - (PlayerScore_Get(scorer, SP_SUICIDES) * suicide_weight);
203
204 // kills minus suicides, calculate weight
205 float playerkillscore = (killcount * 100) * upscaler * frag_weight;
206 // * 100 to avoid float inaccuracy at that decimal level
207
208 float playerscore = roundedplayerdamagescore + playerkillscore;
209
210 // calculated how much score the player has and now calculate total of how much they are supposed to have
211 float scoretoadd = playerscore - (PlayerScore_Get(scorer, SP_SCORE) * 100);
212 // * 100 to avoid float inaccuracy at that decimal level
213
214 // adjust total score to be what the player is supposed to have
215 GameRules_scoring_add_team(scorer, SCORE, floor(scoretoadd / 100));
216 // / 100 to move back to the original decimal level
217
218 #if 0
219 // debug printing
220 if(!IS_BOT_CLIENT(scorer)){
221 print(sprintf("%f", scorer.total_damage_dealt), " scorer.total_damage_dealt \n");
222 print(sprintf("%f", scorer.hitsound_damage_dealt), " scorer.hitsound_damage_dealt \n");
223 print(sprintf("%f", playerdamagescore/100), " playerdamagescore \n");
224 print(sprintf("%f", roundedplayerdamagescore/100), " rounded playerdamagescore \n");
225 print(sprintf("%f", playerkillscore/100), " playerkillscore \n");
226 print(sprintf("%f", PlayerScore_Get(scorer, SP_KILLS)), " PlayerScore_Get(scorer, SP_KILLS) \n");
227 print(sprintf("%f", playerscore/100), " playerscore \n");
228 print(sprintf("%f", scoretoadd/100), " scoretoadd \n");
229 print(sprintf("%f", PlayerScore_Get(scorer, SP_SCORE)), " PlayerScore_Get(scorer, SP_SCORE) \n \n");
230 }
231 #endif
232 return;
233 }
234
235 case 2:
236 {
237 // calculate how much score the player should have based on their frags gotten and then add the missing score
238 float playerkillscore = PlayerScore_Get(scorer, SP_KILLS) - PlayerScore_Get(scorer, SP_TEAMKILLS) - PlayerScore_Get(scorer, SP_SUICIDES);
239 float upscaledplayerscore = playerkillscore * upscaler;
240 float scoretoadd = upscaledplayerscore - PlayerScore_Get(scorer, SP_SCORE);
241 GameRules_scoring_add_team(scorer, SCORE, floor(scoretoadd));
242
243 #if 0
244 // debug printing
245 if(!IS_BOT_CLIENT(scorer)){
246 print(sprintf("%f", playerkillscore), " playerkillscore \n");
247 print(sprintf("%f", PlayerScore_Get(scorer, SP_KILLS)), " PlayerScore_Get(scorer, SP_KILLS) \n");
248 print(sprintf("%f", upscaledplayerscore), " upscaled playerscore \n");
249 print(sprintf("%f", scoretoadd), " scoretoadd \n");
250 print(sprintf("%f", PlayerScore_Get(scorer, SP_SCORE)), " PlayerScore_Get(scorer, SP_SCORE) \n \n");
251 }
252 #endif
253 return;
254 }
255
256 case 3:
257 {
258 // calculate how much score the player should have based on their damage dealt and then add the missing score
259 float playerdamagescore = ((scorer.total_damage_dealt / (start_health + start_armorvalue)) * 100);
260 float roundedplayerdamagescore = rint(playerdamagescore * 10) / 10;
261 float upscaledplayerscore = roundedplayerdamagescore * upscaler;
262 float scoretoadd = upscaledplayerscore - (PlayerScore_Get(scorer, SP_SCORE) * 100);
263 GameRules_scoring_add_team(scorer, SCORE, floor(scoretoadd / 100));
264
265 #if 0
266 // debug printing
267 if(!IS_BOT_CLIENT(scorer)){
268 print(sprintf("%f", scorer.total_damage_dealt), " scorer.total_damage_dealt \n");
269 print(sprintf("%f", playerdamagescore), " playerdamagescore \n");
270 print(sprintf("%f", roundedplayerdamagescore), " rounded playerdamagescore \n");
271 print(sprintf("%f", upscaledplayerscore), " upscaled playerscore \n");
272 print(sprintf("%f", scoretoadd), " scoretoadd \n");
273 print(sprintf("%f", PlayerScore_Get(scorer, SP_SCORE)), " PlayerScore_Get(scorer, SP_SCORE) \n \n");
274 }
275 #endif
276 return;
277 }
278 }
279}
280
281MUTATOR_HOOKFUNCTION(mayhem, PlayerDamage_SplitHealthArmor)
282{
284
286
287 if (StatusEffects_active(STATUSEFFECT_SpawnShield, frag_target) && autocvar_g_spawnshield_blockdamage >= 1) return;
288
289 entity frag_attacker = M_ARGV(1, entity);
290 float frag_deathtype = M_ARGV(6, float);
291 float frag_damage = M_ARGV(7, float);
292 float damage_take = bound(0, M_ARGV(4, float), GetResource(frag_target, RES_HEALTH));
293 float damage_save = bound(0, M_ARGV(5, float), GetResource(frag_target, RES_ARMOR));
294 float excess = max(0, frag_damage - damage_take - damage_save);
295 float total = frag_damage - excess;
296
297 if (total == 0) return;
298
301
302 entity scorer = NULL; // entity which needs their score to be updated
303
304 if (IS_PLAYER(frag_attacker))
305 {
306 // non-friendly fire
307 if (frag_target != frag_attacker)
308 frag_attacker.total_damage_dealt += total;
309
310 // friendly fire aka self damage
312 frag_attacker.total_damage_dealt -= total;
313
314 scorer = frag_attacker;
315 }
316 else
317 {
318 // handle (environmental hazard) suiciding, check first if the player
319 // has a registered attacker who most likely pushed them there to
320 // avoid punishing pushed players as pushers are already rewarded
321 // deathtypes:
322 // kill = suicide, drown = drown in water/liquid, hurttrigger = out of the map void or hurt triggers inside maps like electric sparks
323 // camp = campcheck, lava = lava, slime = slime
324 // team change / rebalance suicides are currently not included
326 frag_deathtype == DEATH_KILL.m_id ||
327 frag_deathtype == DEATH_DROWN.m_id ||
328 frag_deathtype == DEATH_HURTTRIGGER.m_id ||
329 frag_deathtype == DEATH_CAMP.m_id ||
330 frag_deathtype == DEATH_LAVA.m_id ||
331 frag_deathtype == DEATH_SLIME.m_id ||
332 frag_deathtype == DEATH_SWAMP.m_id))
333 frag_target.total_damage_dealt -= total;
334
335 scorer = frag_target;
336 }
337
338 #if 0
339 // debug printing
340 if(!IS_BOT_CLIENT(scorer)){
341 print(sprintf("%f", total), " total dmg from PlayerDamage_SplitHealthArmor \n");
342 print(sprintf("%f", scorer.total_damage_dealt), "scorer.total_damage_dealt\n");
343 }
344 #endif
345
347}
348
350{
351 entity frag_attacker = M_ARGV(0, entity);
352 M_ARGV(2, float) = 0; //score to give for the frag directly
353
354 if (IS_PLAYER(frag_attacker)) MayhemCalculatePlayerScore(frag_attacker);
355
356 return true;
357}
358
359MUTATOR_HOOKFUNCTION(mayhem, reset_map_players)
360{
361 // reset damage dealt on reset
362 FOREACH_CLIENT(true, {
363 it.total_damage_dealt = 0;
364 });
365 return false;
366}
367
const int CBC_ORDER_FIRST
Definition base.qh:10
#define MUTATOR_HOOKFUNCTION(...)
Definition base.qh:335
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
float GetResource(entity e, Resource res_type)
Returns the current amount of resource the given entity has.
const int IT_UNLIMITED_AMMO
Definition item.qh:23
const int IT_UNLIMITED_SUPERWEAPONS
Definition item.qh:24
#define M_ARGV(x, type)
Definition events.qh:17
#define IS_DEAD(s)
Definition player.qh:245
#define IS_PLAYER(s)
Definition player.qh:243
RES_ARMOR
Definition ent_cs.qc:130
float bound(float min, float value, float max)
float cvar(string name)
float rint(float f)
void print(string text,...)
float floor(float f)
float max(float f,...)
#define NULL
Definition post.qh:14
int killcount
Definition client.qh:315
int autocvar_g_pickup_items
Definition items.qh:10
float autocvar_g_spawnshield_blockdamage
Definition player.qh:4
#define PlayerScore_Get(player, scorefield)
Returns the player's score.
Definition scores.qh:42
bool StatusEffects_active(StatusEffect this, entity actor)
float frag_damage
Definition sv_ctf.qc:2322
entity frag_target
Definition sv_ctf.qc:2321
bool autocvar_g_mayhem_powerups
Definition sv_mayhem.qc:13
float autocvar_g_mayhem_start_ammo_nails
Definition sv_mayhem.qc:26
float autocvar_g_mayhem_start_ammo_rockets
Definition sv_mayhem.qc:27
float autocvar_g_mayhem_start_ammo_fuel
Definition sv_mayhem.qc:29
void mayhem_Initialize()
Definition sv_mayhem.qc:41
float autocvar_g_mayhem_point_limit
Definition sv_mayhem.qc:7
void MayhemCalculatePlayerScore(entity scorer)
Definition sv_mayhem.qc:145
bool autocvar_g_mayhem_selfdamage
Definition sv_mayhem.qc:14
string autocvar_g_mayhem_weaponarena
Definition sv_mayhem.qc:12
float autocvar_g_mayhem_start_health
Definition sv_mayhem.qc:23
bool autocvar_g_mayhem_scoring_disable_selfdamage2score
Definition sv_mayhem.qc:18
float autocvar_g_mayhem_start_armor
Definition sv_mayhem.qc:24
float total_damage_dealt
Definition sv_mayhem.qc:31
bool autocvar_g_mayhem_unlimited_ammo
Definition sv_mayhem.qc:21
bool autocvar_g_mayhem_pickup_items
Definition sv_mayhem.qc:19
bool autocvar_g_mayhem_rot
Definition sv_mayhem.qc:11
float autocvar_g_mayhem_start_ammo_shells
Definition sv_mayhem.qc:25
float autocvar_g_mayhem_scoring_kill_weight
Definition sv_mayhem.qc:17
float autocvar_g_mayhem_point_leadlimit
Definition sv_mayhem.qc:8
bool autocvar_g_mayhem_pickup_items_remove_weapons_and_ammo
Definition sv_mayhem.qc:20
float autocvar_g_mayhem_start_ammo_cells
Definition sv_mayhem.qc:28
float autocvar_g_mayhem_scoring_damage_weight
Definition sv_mayhem.qc:16
bool autocvar_g_mayhem_regenerate
Definition sv_mayhem.qc:10
float autocvar_g_mayhem_scoring_upscaler
Definition sv_mayhem.qc:15
int autocvar_g_powerups
Definition sv_powerups.qh:7
void GameRules_limit_score(int limit)
Definition sv_rules.qc:23
void GameRules_limit_lead(int limit)
Definition sv_rules.qc:33
#define GameRules_scoring_add_team(client, fld, value)
Definition sv_rules.qh:89
float autocvar_g_tmayhem_scoring_damage_weight
Definition sv_tmayhem.qh:8
float autocvar_g_tmayhem_scoring_upscaler
Definition sv_tmayhem.qh:6
float autocvar_g_tmayhem_scoring_kill_weight
Definition sv_tmayhem.qh:7
bool autocvar_g_tmayhem_scoring_disable_selfdamage2score
Definition sv_tmayhem.qh:9
bool teamplay
Definition teams.qh:59
#define FOREACH_CLIENT(cond, body)
Definition utils.qh:50
#define IS_BOT_CLIENT(v)
want: (IS_CLIENT(v) && !IS_REAL_CLIENT(v))
Definition utils.qh:15
float warmup_start_ammo_cells
Definition world.qh:105
float start_ammo_shells
Definition world.qh:84
float warmup_start_ammo_rockets
Definition world.qh:104
float warmup_start_ammo_shells
Definition world.qh:102
float start_ammo_fuel
Definition world.qh:88
int start_items
Definition world.qh:83
float warmup_start_ammo_nails
Definition world.qh:103
float start_ammo_cells
Definition world.qh:87
float warmup_start_health
Definition world.qh:107
float start_ammo_rockets
Definition world.qh:86
float start_armorvalue
Definition world.qh:97
float warmup_start_ammo_fuel
Definition world.qh:106
float start_health
Definition world.qh:96
float warmup_start_armorvalue
Definition world.qh:108
float start_ammo_nails
Definition world.qh:85