Forum

> > CS2D > Scripts > How does mousemap work?
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch How does mousemap work?

10 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Re: How does mousemap work?

DC
Admin Off Offline

Zitieren
cs2d lua cmd player mousex and mousey represent the current mouse position on the screen (0 to screenWidth for X and 0 to screenHeight for Y).
mousemapx and mousemapy represent the current mouse position in the game world (relative to the currently visible part of the map).

So the math is:
1
2
mouseMapX = ownPlayerX - screenWidth/2 + mouseX
mouseMapY = ownPlayerY - screenHeight/2 + mouseY

The screenWidth/2 and screenHeight/2 part is because the player is at the center of the screen.

Rule of thumb:
• Use mousex/mousey if you want to display HUD images/texts at the mouse position
• Use mousemapx/mousemapy for everything else e.g. spawning things on the map at the mouse position.

alt Re: How does mousemap work?

The Dark Shadow
User Off Offline

Zitieren
Thanks @user DC, One more thing so now I want whenever my pointer is on my enemy team then it should return true, else return false. I'm currently using this code but it's not working properly.

1
2
3
4
5
6
7
8
9
10
11
local mouseMapX = player(myID, "x") - player(myID, "screenw")/2 + player(myID, "mousex")
local mouseMapY = player(myID, "y") - player(myID, "screenh")/2 + player(myID, "mousey")

addhook("second", "OnSecond")
OnSecond = (function()
	if (mouseMapX == player(enemyID, "x") and mouseMapY == player(enemyID, "y")) then
		msg("Yes")
	else
		msg("No")
	end
end)

Would be appreciated

alt Re: How does mousemap work?

DC
Admin Off Offline

Zitieren
1. Maybe you didn't understand that there already is "mousemapx" and "mousemapy"? Otherwise... why aren't you using cs2d lua cmd player(id "mousemapx") and cs2d lua cmd player(id "mousemapy")?!
You don't have to (and you shouldn't!) calculate that stuff yourself. There's mousemapx and y so you don't have to do it yourself!

2. You are only calculating the position once. You would have to do it each time you want to check it (inside the hook) because it's changing all the time.

3. The position is in pixels. When you are comparing pixel coordinates you would have to point exactly at one specific pixel of an enemy to make that stuff work. You should calculate the distance between player and mouse and check if that is below a certain threshold like 20 pixels or something.

alt Re: How does mousemap work?

The Dark Shadow
User Off Offline

Zitieren
Thanks for the tips, But user DC I have no idea how to do that

user DC hat geschrieben
3. The position is in pixels. When you are comparing pixel coordinates you would have to point exactly at one specific pixel of an enemy to make that stuff work. You should calculate the distance between player and mouse and check if that is below a certain threshold like 20 pixels or something.


1
2
3
4
5
6
7
8
addhook("always", "OnAlways")
OnAlways = (function()
	if (player(myID, "mousemapx") == player(enemyID, "x") and player(myID, "mousemapy") == player(enemyID, "y")) then
		parse("hudtxt 0 Yes")
	else
		parse("hudtxt 0 No")
	end
end)

alt Re: How does mousemap work?

Cure Pikachu
User On Online

Zitieren
You are close. Now it's just a matter of getting those IDs (probably by nested looping of player tables if you plan to do this for every player) and also accounting for the fact that you are highly unlikely to have your cursor point dead center at the enemy player's coordinates (which is why user DC suggested you check if it's within a threshold instead of being exact).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
addhook("always", "OnAlways")
function onAlways()
	--[[
	Some way to get myID and enemyID
	]]
	-- Check if it's within 20 pixel threshold
	local inrangex, inrangey
	inrangex = player(myID,"mousemapx") >= player(enemyID,"x")-10 and player(myID,"mousemapx") <= player(enemyID,"x")+10
	inrangey = player(myID,"mousemapy") >= player(enemyID,"y")-10 and player(myID,"mousemapy") <= player(enemyID,"y")+10
	if inrangex and inrangey then
		parse("hudtxt 0 Yes")
	else
		parse("hudtxt 0 No")
	end
end

alt Re: How does mousemap work?

DC
Admin Off Offline

Zitieren
You really NEVER should use cs2d lua hook always for things which update UI stuff. That will cause TONS of traffic and lead to very bad conditions when many players are on the server.

I guess cs2d lua hook ms100 (that's 10 times a second instead of 50 times a second) should be ok too.

Even better would be to save the displayed state and to only call that parse("hudtxt ...") (or whatever you do to visualize things on the client side) when the displayed stuff really changed.

alt Re: How does mousemap work?

Mami Tomoe
User Off Offline

Zitieren
user DC hat geschrieben
I guess cs2d lua hook ms100 (that's 10 times a second instead of 50 times a second) should be ok too.


Isn't the always hook running 60 times a second?
Also it's sometimes a little higher (62~) or lower.

alt Re: How does mousemap work?

DC
Admin Off Offline

Zitieren
@user Mami Tomoe: It makes sense to assume that due to the increased "new" FPS cap at 60 but no, it has been capped at 50 executions in order to keep it compatible with older CS2D versions which had the 50 FPS cap.
It's documented here cs2d lua hook always
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht