Forum

> > CS2D > Scripts > Hud image on map issue
Forums overviewCS2D overview Scripts overviewLog in to reply

English Hud image on map issue

4 replies
To the start Previous 1 Next To the start

old Hud image on map issue

Mora OP
User Off Offline

Quote
Hello, I have some idea to do the thing:
I throw flashbang, it show me who's blind(script works fine)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
addhook("projectile","flash_projectile")

function flash_projectile(id,weapon,x,y,projectileid)

    if weapon ~= 52 then return end

    local blinded_ids = {}
    local not_blinded_ids = {}

    local screen_w = 850 / 2
    local screen_h = 480 / 2

    for _,pid in pairs(player(0,"tableliving")) do
        if pid ~= id then
            local px = player(pid,"x")
            local py = player(pid,"y")

            if math.abs(px - x) <= screen_w and math.abs(py - y) <= screen_h then
                local raycheck = raycast(x,y,px,py)
				if raycheck == true then
                    table.insert(blinded_ids, player(pid,"name"))
                else
                    table.insert(not_blinded_ids, player(pid,"name"))
                end
            end
        end
    end

    if #blinded_ids > 0 then
        msg("Blinded: "..table.concat(blinded_ids,", "))
    end
    if #not_blinded_ids > 0 then
        msg("On screen, but not blinded: "..table.concat(not_blinded_ids,", "))
    end
end

function raycast(x1, y1, x2, y2)
    local dx = x2 - x1
    local dy = y2 - y1
    local dist = math.sqrt(dx*dx + dy*dy)

    local steps = math.floor(dist / 16)
    if steps < 1 then steps = 1 end

    local stepx = dx / steps
    local stepy = dy / steps

    local cx, cy = x1, y1

    for i = 1, steps do
        cx = cx + stepx
        cy = cy + stepy

        local tx = math.floor(cx / 32)
        local ty = math.floor(cy / 32)

        if tile(tx, ty, "wall") then
            return false
        end
    end

    return true
end

i stuck with image mode. Can i create sprite as a hud but always rendering map position while being flashed? what image mode i can use for that? Don't forget it must be not refreshed by script. And also there is a way create such image over flash screen effect? The main idea is like having this "eye" sprite while somebody is flashed(Spectator mode).

"Blindfucker Hero":
lvl 1 - Eye sense) Throw 10 flashbangs in total. Unlocks: Blinded player info msg.
lvl 2 - Blindmaster) Throw 500 flashbangs in total. Unlocks: On screen, but not blinded players info.
lvl 3 - Blindfucker) Throw 1000 flashbangs in total. Unlocks: hud image added on screen "gfx/Mora/Blindfucker/eye.png" centre to see urself when flashed.
lvl 4 - Blindfucker-I) Flash 100 players in total. Unlocks: Show all players last pos on ur screen when flashed by own flashbang
lvl 5 - Blindfucker-II) Flash 500 players in total. Unlocks: Show all players last pos on ur screen when flashed by others flashbang
lvl 6 - Blindfucker-Master sense I) Throw 1500 flashbangs in total. Unlocks: Update blinded players pos after 500 ms when flashed by self grenade
lvl 7 - Blindfucker-Master sense II) Flash 1000 players in total. Unlocks: Updated blined players pos after 500 ms when flashed by others grenade.
etc.... As you may see is required to work with hud images being flashed, mode 2 doesn't fit requirements.

This must be the good new one image mode that created this way without calculating it via scripts maybe image mode 5? (HUD image (covering everything, part of the interface, affected by mp_hudscale and uses map x/y coordinates)
edited 1×, last 16.02.26 07:58:15 am

old Re: Hud image on map issue

Gaios
Reviewer Off Offline

Quote
I can't really help you with the image/HUD stuff though — I'm not sure what are you talking about.

But I took a look at your raycast and it has some problems so I rewrote it. I renamed it to has_line_of_sight because technically a "raycast" should return hit position, surface normal, distance etc. Yours just checks if the path between two points is clear or not, so the name fits better. I also added an optional max_dist parameter in case you need it.

Why your version has issues:

Your raycast walks along the line in fixed 16px steps. The problem is — it can skip tiles completely. If the ray clips through a corner of a wall at a steep angle, a 16px step can jump right over it. So you get false positives — players behind thin walls or around corners get detected as "blinded" when they shouldn't be.

Also the step count depends on pixel distance, not how many tiles the ray actually crosses. So for long rays you do way more checks than needed, and for diagonal rays through narrow gaps you might do too few.

What the new version does differently:

It uses DDA (Digital Differential Analyzer). Instead of walking in fixed pixel steps, it calculates exactly when the ray hits the next tile boundary — separately for the X axis and Y axis. Then it always picks whichever boundary comes first. This way it visits every single tile on the ray's path. Nothing gets skipped, ever.

It's also a bit faster in most cases. Yours always does dist/16 iterations no matter what. DDA does exactly as many steps as there are tiles to cross. For a horizontal ray across 10 tiles — 10 steps instead of 20.

Code >


> https://www.youtube.com/watch?v=n67cWAQg3zE

Drop-in replacement in your flash_projectile function — just change raycast(x,y,px,py) to has_line_of_sight(x,y,px,py) and it works the same way. Or you can pass a max distance like has_line_of_sight(x,y,px,py,500) if you want to limit range.

Hope this helps with the raycast part at least. Good luck with the image stuff.

old Re: Hud image on map issue

Mora OP
User Off Offline

Quote
@user Gaios: Well the script is just an example to see if I can do that check. The next step was about to find a way drawing images as hud over flash effect(like hudtxt 2) but rendered x,y as map coordinates. Or draw hudtxt 2 rendered x,y as map coordinates. Lua does not fit that - with ping moments it cause stop refreshing player pos and it looks shit.

Damn ur laser looks so insane and smooth! Im very appreciated You for that good code to learn!

/I wish one day cs2d breaks modding limits

old Re: Hud image on map issue

reverend_insanity
User Off Offline

Quote
You mean something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
local HUD_W = 850
local HUD_H = 480
local hudScale = tonumber(game("mp_hudscale"))

function AdjustScaleFactor()
    hudScale = tonumber(game("mp_hudscale"))
end

local function GetScale(resolution_w, resolution_h)
    if hudScale == 0 then
        return 1 / (HUD_W / resolution_w), 1 / (HUD_H / resolution_h) -- 1.6 for 768
    elseif hudScale == 1 then -- hudscale = resolution
        return 1, 1
    end

    print("Uncalculated scale:", hudScale)
    return nil
end

-- test this
function translateToRelativePos(from_x, from_y, target_x, target_y, x_offset, y_offset, resolution_w, resolution_h) -- from player target PosX, PosY to HudX, HudY
    local CENTER_X = HUD_W / 2
    local CENTER_Y = HUD_H / 2

    local scaledx, scaledy = GetScale(resolution_w, resolution_h)
    local hudx = (CENTER_X + (target_x - from_x) - (x_offset or 0)) * scaledx -- starts from center (player position) + adds distance difference + add offset
    local hudy = (CENTER_Y + (target_y - from_y) - (y_offset or 0)) * scaledy

    -- draw only if on screen
    if hudx >= 0 and hudx <= HUD_W*scaledx and hudy >= 0 and hudy <= HUD_H*scaledy then
        return { x = hudx, y = hudy }
    end

    return nil
end

function translateToMousePos(pos_x, pos_y, resolution_w, resolution_h) -- from player mouseX, mouseY to HudX, HudY
    local scaledx, scaledy = GetScale(resolution_w, resolution_h)

    local hudx = pos_x * scaledx
    local hudy = pos_y * scaledy

    return { x = hudx, y = hudy }
end

Here's a DEMO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
damageIndicator = {}
damageIndicator.HUD_START = 50
damageIndicator.HUD_END   = 199
damageIndicator.CURRENT_HUD_INDEX = 50

function damageIndicator.showDamage(viewer, hud_index, x, y, hp)
    -- animationManager.start(
    --     viewer,
    --     hud_index,
    --     {
    --         text = "\169255000000"..hp.." HP",
    --         x = x-5,
    --         y = y-30,
    --         steps = {
    --             HudAnimations.move({duration = 300, stop_duration = 0, target_x = x-5, target_y = y-50}), -- used before scaling
    --             HudAnimations.fade({duration = 400, stop_duration = 0, target_alpha = 0}),
    --             HudAnimations.scale({duration = 300, stop_duration = 0, target_size = 20})
    --         }
    --     }
    -- )
   parse('hudtxt2 '..viewer..' '..hud_index..' "\169255000000'..hp..' HP" '..x..' '..y) -- here for you
 -- for simplicity's sake
end

-- For spectators: You can modify this so it shows to you relative to the player your spectating on
-- You can't use this if in free spectator mode (no specific player) because there's no way to get spectator X,Y

function damageIndicator.hit(victim_x, victim_y, hpdmg) -- show to all living people
    local current_hud_index = damageIndicator.CURRENT_HUD_INDEX
    if current_hud_index == damageIndicator.HUD_END then
        current_hud_index = damageIndicator.HUD_START
    else
        current_hud_index = current_hud_index + 1
    end

    damageIndicator.CURRENT_HUD_INDEX = current_hud_index
    
    for _, viewer in pairs(player(0, "tableliving")) do -- only show to the living
        if not player(viewer, "bot") then
            local relativepos = translateToRelativePos(player(viewer, "x"), player(viewer, "y"), victim_x, victim_y, -2, 0, player(viewer, "screenw"), player(viewer, "screenh"))

            -- show your hud in relative.x, relative.y
            damageIndicator.showDamage(viewer, current_hud_index, relativepos.x, relativepos.y, hpdmg)
        end
    end
end

The damage hud only appears once and won't move as you move in this script, you have to set some kind of timer manager for the hud and check it with ms100 or always, if not passed yet, show again with same hud_index
edited 1×, last 16.02.26 12:49:11 pm

old Re: Hud image on map issue

Mora OP
User Off Offline

Quote
Yeah I have already done it via scripts yesterday, but with higher ping or lag spikes the visual part also delayed. hud-world x,y client-sided hudtxt3 processing would be awesome. This way u can create whatever individual hudtxt on map(there has some such scripts but is all it's maximum - scale screen hud x,y with lua and ping delay)
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview