Skip to content

Commit 8f169c8

Browse files
committed
fix damage calc
1 parent 23de3a1 commit 8f169c8

8 files changed

Lines changed: 322 additions & 15 deletions

File tree

‎apps/game/src/game-utility.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ export class GameUtility {
738738
damage = result.healthDamage;
739739
actor.armor = result.armor;
740740
}
741-
actor.health = Math.max(0, actor.health - damage);
741+
actor.health = Math.max(0, actor.health - Math.floor(damage));
742742
if (id === "player") {
743743
g.damage = 0.2;
744744
if (damage > 0) g.groundInventory.invalidateEquipment();

‎apps/game/src/game.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ import {
5959
nextLossLevel,
6060
addMoney,
6161
} from "./economy";
62-
import { applyArmorDamage, friendlyFireScale } from "./damage";
62+
import { applyArmorDamage, friendlyFireScale, hitRegion } from "./damage";
6363
import { dust2BuyZones, dust2BombSites } from "./buy-zones";
6464
import { groundCorrectedBombSample } from "./bomb-damage";
6565
import {
@@ -1505,8 +1505,7 @@ export class Game {
15051505
const victim = actor === "player" ? this : actor;
15061506
if (this.teamMode && victim.team === attackerTeam)
15071507
damage *= friendlyFireScale(this.competitive ? "competitive" : "casual", kind);
1508-
const height = (data.height ?? 1.1) * (data.crouch ? 1.65 / 1.1 : 1);
1509-
const region = data.head ? "head" : height < 0.85 ? "leg" : height < 1.08 ? "stomach" : "chest";
1508+
const region = hitRegion(!!data.head, data.height ?? 1.1, data.crouch);
15101509
const result = applyArmorDamage(
15111510
damage,
15121511
region,
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import { expect, test } from "@playwright/test";
2+
import type { Game } from "../src/game";
3+
declare global {
4+
interface Window {
5+
__game: Game;
6+
}
7+
}
8+
test("AK headshot damage on the native A-site ramp line", async ({ page }) => {
9+
test.setTimeout(180000);
10+
await page.goto("/");
11+
await expect(page.locator("#play")).toBeEnabled({ timeout: 120000 });
12+
await page.locator("#play").evaluate((button) => {
13+
button.onclick = () => window.__game.start("practice");
14+
});
15+
await page.locator("#home-play").click();
16+
await page.locator("#play").click();
17+
await page.waitForFunction(() => window.__game.locked);
18+
await page.waitForLoadState("networkidle");
19+
const result = await page.evaluate(() => {
20+
const g = window.__game;
21+
g.freezeTime = 0;
22+
g.roundPause = 10000;
23+
g.health = 1e6;
24+
g.updateBots = () => {};
25+
g.moveBots = () => {};
26+
g.bots.forEach((bot) => {
27+
bot.group.position.x += 100;
28+
});
29+
g.buy(0);
30+
g.reload = g.drawTime = g.cooldown = 0;
31+
const victim = g.bots.find((bot) => bot.team !== g.team)!;
32+
const victimIndex = g.bots.indexOf(victim);
33+
const hits: { damage: number; head: boolean | undefined }[] = [];
34+
const damage = g.damageActor.bind(g);
35+
g.damageActor = (actor, amount, data, ...rest) => {
36+
hits.push({ damage: amount, head: data.head });
37+
damage(actor, amount, data, ...rest);
38+
};
39+
const sounds: string[] = [];
40+
g.sound = (cue) => {
41+
sounds.push(cue);
42+
};
43+
const nav = g.dust!.navigation;
44+
const rows = [];
45+
for (const source of [1902, 1930, 1952])
46+
for (const target of [1896, 1905, 1938]) {
47+
const from = nav.areas.find((area) => area.id === source)!.center;
48+
const to = nav.areas.find((area) => area.id === target)!.center;
49+
g.position.fromArray(from);
50+
g.velocity.x = g.velocity.z = 0;
51+
g.vertical = 0;
52+
g.eye = 1.65;
53+
victim.health = 100;
54+
victim.armor = 100;
55+
victim.helmet = true;
56+
victim.group.position.fromArray(to);
57+
victim.group.updateMatrixWorld(true);
58+
const head = victim.rig.head.getWorldPosition(g.position.clone());
59+
head.y += 0.04;
60+
const eye = g.position.clone();
61+
eye.y += g.eye;
62+
const direction = head.clone().sub(eye).normalize();
63+
g.yaw = Math.atan2(-direction.x, -direction.z);
64+
g.pitch = Math.asin(direction.y);
65+
g.aimPunch.pitch = g.aimPunch.yaw = 0;
66+
g.weaponRecoil[0].index = 0;
67+
g.ammo = 30;
68+
g.cooldown = g.drawTime = g.shotTime = 0;
69+
g.ray.set(eye, direction);
70+
g.ray.far = eye.distanceTo(head) + 0.5;
71+
const cover = g.dust!.collision.intersections(g.ray.ray, g.ray.far).map((impact) => ({
72+
distance: impact.distance,
73+
material: impact.object.userData.bulletMaterial,
74+
name: impact.object.name,
75+
}));
76+
hits.length = sounds.length = 0;
77+
const random = Math.random;
78+
Math.random = () => 0;
79+
try {
80+
g.fire();
81+
} finally {
82+
Math.random = random;
83+
}
84+
rows.push({
85+
source,
86+
target,
87+
distance: eye.distanceTo(head),
88+
cover,
89+
hits: [...hits],
90+
sounds: [...sounds],
91+
health: victim.health,
92+
victimIndex,
93+
});
94+
}
95+
return rows;
96+
});
97+
console.log("A-SITE DAMAGE AUDIT", JSON.stringify(result));
98+
const clear = result.filter((row) => row.cover.length === 0 && row.hits.some((hit) => hit.head));
99+
expect(clear.length).toBeGreaterThan(0);
100+
for (const row of clear) expect(row.health, JSON.stringify(row)).toBe(0);
101+
const utility = await page.evaluate(() => {
102+
const g = window.__game,
103+
victim = g.bots.find((bot) => bot.team !== g.team)!;
104+
victim.group.position.copy(g.position).x += 2;
105+
g.visible = () => true;
106+
return ["he", "decoy", "molotov", "incendiary"].map((kind) => {
107+
victim.health = 100;
108+
victim.armor = kind === "he" || kind === "decoy" ? 0 : 100;
109+
const position = victim.group.position.clone();
110+
position.y += 0.8;
111+
g.utility.areaDamage(
112+
{
113+
kind,
114+
owner: "player",
115+
team: g.team,
116+
position,
117+
cells: [victim.group.position.clone()],
118+
} as never,
119+
2.9,
120+
1.5,
121+
);
122+
return { kind, health: victim.health, armor: victim.armor };
123+
});
124+
});
125+
expect(utility).toEqual([
126+
{ kind: "he", health: 98, armor: 0 },
127+
{ kind: "decoy", health: 98, armor: 0 },
128+
{ kind: "molotov", health: 98, armor: 100 },
129+
{ kind: "incendiary", health: 98, armor: 100 },
130+
]);
131+
});

‎docs/weapon-data.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ Ground Rebel uses its imported native bone-local bullet capsules; the older CT r
3838

3939
Knife rules follow the historical implementation: front initial/repeated slash 40/25, front stab 65, rear slash/stab 90/180 (153 heavy backstab damage through full armor), 85% armor penetration and 48/32-Source-unit reach. Miss/hit recovery is 0.4/0.5 seconds for slash and 1/1.1 for stab. Impact phases and the forgiving trace fan remain reconstructed. Native visual clip seconds are independent; see [assets.md](assets.md#knife-timing). Rear hits use the victim’s visible body direction rather than an AI turn target. Heavy hits use the native `Weapon_Knife.Hit.Heavy` cue for local and remote players. Knives are Inventory equipment, never buy-menu purchases.
4040

41+
## Damage regression audit
42+
43+
All 34 firearm damage values, head multipliers, armor ratios, penetration powers, ranges, range modifiers and pellet counts were checked against the pinned `weapons.vdata` SHA-256. `packages/simulation/src/damage-audit.test.ts` exercises authoritative damage for standing/crouched head, chest, stomach and leg hits, armor/helmet states, shotgun pellets, friendly-fire scaling and AK range/cover cases. Offline A-site back-to-ramp traces are covered by `apps/game/tests/damage-audit.spec.ts`.
44+
45+
A clean enemy AK helmet headshot deals 111 damage at point-blank range and about 109 at 12.7 m. It is lethal at normal A-site distances; cover penetration, very long range (roughly 69 m onward against full armor) or competitive team damage can make a headshot survivable. The headshot cue confirms a head hit, not a kill. No unconditional one-shot override is applied.
46+
47+
The audit corrected multiplayer vest protection on leg hits, knife friendly-fire scaling (other damage rather than bullet damage), and offline utility health rounding to match the authoritative server. Fire bypasses armor; HE/decoy use their shared armor calculation. C4's baked query already returns health damage and is not armor-reduced twice. Utility falloff/propagation and native-body hitbox approximations remain as documented above.
48+
4149
## Native Dust II data
4250

4351
`dust2-buy-zones.json` retains the pinned map's buy and bomb-target hulls. The importer verifies box vertices/planes before reducing them to bounds. Membership uses actor-hull contact; time, team, balance and loadout checks remain separate.

‎packages/simulation/src/bullet-solver.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as T from "three";
22
import { penetrate, rangeDamage, SOURCE_UNITS_PER_METER } from "./ballistics";
33
import type { BulletMaterial } from "./ballistics";
4+
import { hitRegion, hitRegionMultiplier } from "./damage";
45

56
export type BulletStats = { damage: number; power: number; range: number; modifier: number };
67
export type BulletSurface<Data> = {
@@ -70,8 +71,7 @@ export function hitgroupMultiplier(
7071
headMultiplier: number,
7172
crouch = false,
7273
) {
73-
const standingHeight = crouch ? height * (1.65 / 1.1) : height;
74-
return head ? headMultiplier : standingHeight < 0.85 ? 0.75 : standingHeight < 1.08 ? 1.25 : 1;
74+
return hitRegionMultiplier(hitRegion(head, height, crouch), headMultiplier);
7575
}
7676

7777
export function fleshThickness(direction: T.Vector3, yaw: number, head: boolean) {

‎packages/simulation/src/combat.ts‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { MatchEvent, PlayerState } from "@cs2go/protocol";
33
import type { Match } from "./match";
44
import { rayBox, dropWeapon } from "./match";
55
import { weapons, movement, isKnife } from "./physics";
6-
import { applyArmorDamage, friendlyFireScale } from "./damage";
6+
import { applyArmorDamage, friendlyFireScale, hitRegion, type HitRegion } from "./damage";
77
import { addMoney, killReward } from "./economy";
88
import { ballistics } from "./ballistics";
99
import {
@@ -76,17 +76,12 @@ export function damagePlayer(
7676
weapon: number,
7777
armorRatio = weapons[weapon]?.armorRatio ?? 1,
7878
ignoreArmor = false,
79+
region: HitRegion = headshot ? "head" : "chest",
7980
): MatchEvent[] {
8081
if (victim.health <= 0 || damage <= 0) return [];
8182
const result = ignoreArmor
8283
? { armor: victim.armor, healthDamage: damage }
83-
: applyArmorDamage(
84-
damage,
85-
headshot ? "head" : "chest",
86-
victim.armor,
87-
victim.helmet,
88-
armorRatio,
89-
);
84+
: applyArmorDamage(damage, region, victim.armor, victim.helmet, armorRatio);
9085
if (result.healthDamage > 0)
9186
match.receipts.get(victim.id)!.equipment = match.receipts
9287
.get(victim.id)!
@@ -194,7 +189,7 @@ export function shoot(
194189
stab,
195190
memory.knifeAge > 0.9,
196191
knifeBackstab(shooter, victim, victim.yaw + Math.PI),
197-
) * (victim.team === shooter.team ? friendlyFireScale(match.state.mode, "bullet") : 1),
192+
) * (victim.team === shooter.team ? friendlyFireScale(match.state.mode, "other") : 1),
198193
false,
199194
shooter.weapon,
200195
1.7,
@@ -269,6 +264,9 @@ export function shoot(
269264
damage,
270265
!!data.head,
271266
shooter.weapon,
267+
weapon.armorRatio,
268+
false,
269+
hitRegion(!!data.head, data.height!, data.victim.eye < 1.4),
272270
);
273271
for (const death of deaths)
274272
if (death.kind === "kill") {

0 commit comments

Comments
 (0)