evasivedocs
CS2 scriptingLua 5.4.9 · 25 guides · 292 API entries
Advanced

Map traces and the world

world lets a script ask questions about the map itself: can this point see that one, where does a line hit a wall, how much damage would a bullet do through it, where will a grenade land. It also gives you the smokes and the minimap.

Needs: Allow game scripting. Everything here is read only.

The map has to be loaded

Traces run against the map's collision mesh. world.ready() is true once it is loaded. Props, doors, players and smoke do not block traces.

Without a loaded map the trace functions give nil and a reason. Check before you use the result:

Lua
events.on("tick", function()
    if not world or not world.ready() then return end
    -- traces are safe from here on
end)

Budgets

Traces cost time, so each script gets a fixed number per logic pass, which is one tick and the paint after it:

What Per logic pass
Rays: world.visible, world.trace, world.through_smoke and Player:is_visible 256
Wallbang checks: world.penetration 32
Grenade predictions: world.predict_grenade 4

Past the limit the call gives nil and "ray budget", "penetration budget" or "prediction budget". Do traces in tick, keep the results, and draw them in paint.

Line of sight

world.visible(from, to) is true when no map triangle lies between the two points:

Lua
-- @name Visible enemies
-- @description Green names for enemies you can see, grey for the rest.

local seen = {}

events.on("tick", function()
    seen = {}
    if not world or not world.ready() then return end
    local me = entities.local_player()
    if not me or not me.alive then return end
    for _, p in ipairs(entities.players("enemies")) do
        if p.alive then
            seen[p.index] = world.visible(me.eye_pos, p.eye_pos) == true
        end
    end
end)

events.on("paint", function()
    if not game or not game.connected() then return end
    for _, p in ipairs(entities.players("enemies")) do
        if p.alive then
            local x, y = p:bone_screen(bone.head)
            if x then
                local col = seen[p.index] and color(120, 230, 120) or color(150, 150, 150)
                draw.text(x, y - 18, p.name, col, {align = "center", font = "esp", outline = true})
            end
        end
    end
end)

p:is_visible() does a fuller check from your eyes to the head, chest, pelvis, hands and feet, the same one the ESP uses. It is cached per frame and counts as one ray.

Where a line hits

world.trace(from, to) gives the first surface on the way: hit, pos, normal, dist, fraction from 0 to 1, and surface, the material name. When nothing is in the way, hit is false and pos is the end point.

This one marks the wall you are looking at and names its material:

Lua
local hit = nil

local function forward(angles)
    local pitch, yaw = math.rad(angles.x), math.rad(angles.y)
    return vec3(math.cos(pitch) * math.cos(yaw), math.cos(pitch) * math.sin(yaw), -math.sin(pitch))
end

events.on("tick", function()
    hit = nil
    if not world or not world.ready() then return end
    local cam = game.camera()
    if not cam then return end
    local t = world.trace(cam.origin, cam.origin + forward(cam.angles) * 4096)
    if t and t.hit then hit = t end
end)

events.on("paint", function()
    if not hit then return end
    draw.world_circle(hit.pos, 8, ui.accent(), 2)
    draw.world_text(hit.pos, hit.surface .. string.format(" %.0f u", hit.dist), color(255, 255, 255))
end)

Angles are in degrees: x is the pitch, positive looking down, and y is the yaw.

Through walls

world.penetration(from, to, target, hitgroup) runs your current weapon's bullet through the map, the same model the built in wallbang check uses, and applies the target's armour. It gives ok, damage, walls, thickness and reason.

Lua
local damage = {}

events.on("tick", function()
    damage = {}
    if not world or not world.ready() then return end
    local me = entities.local_player()
    if not me or not me.alive then return end
    for _, p in ipairs(entities.players("enemies")) do
        if p.alive then
            local r = world.penetration(me.eye_pos, p.eye_pos, p, hitgroup.head)
            if r and r.ok then damage[p.index] = r.damage end
        end
    end
end)

With 32 checks per pass this is fine for a full team. Draw damage[p.index] next to each player in paint.

Smokes

world.smokes() lists active smokes as {center, radii, density}. world.through_smoke(from, to) is true when the line crosses one. It is the same check aim and trigger use, and it counts as one ray.

Lua
local smoked = {}

events.on("tick", function()
    smoked = {}
    if not world then return end
    local me = entities.local_player()
    if not me or not me.alive then return end
    for _, p in ipairs(entities.players("enemies")) do
        if p.alive then smoked[p.index] = world.through_smoke(me.eye_pos, p.eye_pos) == true end
    end
end)

Where a grenade lands

world.predict_grenade(origin, velocity, kind, remaining) simulates a grenade's flight at 64 ticks a second, bouncing off the map. It gives points (one vec3 per tick), bounces, end_pos and duration in seconds. kind is "he", "flash", "smoke", "molotov", "incendiary" or "decoy". For a grenade already in the air, pass remaining, the seconds left on its fuse.

This script draws the path of every grenade in the air, up to four at a time:

Lua
-- @name Grenade paths
-- @description Where the grenades in the air will land.

local col = ui.color("Path", color(255, 255, 255, 180))
local arcs = {}

local function in_air(g)
    if g.kind == "smoke" then return not g.active end
    if g.kind == "he" or g.kind == "flash" or g.kind == "decoy" then return not g.detonated end
    return g.kind == "molotov"
end

events.on("tick", function()
    arcs = {}
    if not world then return end
    for _, g in ipairs(entities.projectiles()) do
        if #arcs >= 4 then break end
        if in_air(g) then
            local fuse = (g.kind == "he" or g.kind == "flash") and g.remaining or nil
            local arc = world.predict_grenade(g.origin, g.velocity, g.kind, fuse)
            if arc then arcs[#arcs + 1] = arc end
        end
    end
end)

events.on("paint", function()
    if not game then return end
    local c = col:get()
    for _, arc in ipairs(arcs) do
        local pts = arc.points
        for i = 2, #pts do
            draw.world_line(pts[i - 1], pts[i], c, 2)
        end
        draw.world_circle(arc.end_pos, 12, ui.accent(), 2)
    end
end)

The minimap

world.overview() gives CS2's own radar image for the map: image and, on maps with two floors, lower_image, ready for draw.image. It is nil while it loads, and the first call starts the loading.

To place a world position on the image, use x, y and scale. The image is 1024 pixels across:

Lua
-- @name Mini radar
-- @description The map overview with enemy dots.

local SIZE = 256

events.on("paint", function()
    if not world or not game.connected() then return end
    local ov = world.overview()
    if not ov then return end
    local x0, y0 = 20, 200
    draw.image(ov.image, x0, y0, SIZE, SIZE)
    local k = SIZE / 1024
    for _, p in ipairs(entities.players("enemies")) do
        if p.alive then
            local u = (p.origin.x - ov.x) / ov.scale * k
            local v = (ov.y - p.origin.y) / ov.scale * k
            draw.circle_filled(x0 + u, y0 + v, 3, color(255, 80, 80))
        end
    end
end)

On maps with two floors, has_lower is true and split_z is the height between them: draw lower_image for players below it.