English How does mousemap work?

10 replies
Goto Page
To the start Previous 1 Next To the start
27.04.20 06:18:38 pm
Up
The Dark Shadow
User
Offline Off
Yo, How do mousemapx, mousemapy work? What's the formula to change mousepos to mousemappos?
27.04.20 06:54:20 pm
Up
DC
Admin
Offline Off
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:
Code:
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.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
27.04.20 07:37:10 pm
Up
The Dark Shadow
User
Offline Off
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.

Code:
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
27.04.20 08:23:27 pm
Up
DC
Admin
Offline Off
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.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
27.04.20 09:01:59 pm
Up
Masea
Super User
Offline Off
Furthermore, I wouldn't use cs2d lua hook second for that because it would not be precise and accurate, use one which gets triggered more frequently, like cs2d lua hook ms100 or cs2d lua hook always, or maybe even create your own cs2d lua cmd timer.
Create your UI easy and fast: UI Framework | Go deeper into the darkness and test your bravery: file cs2d Outlast II Modification (28)
29.04.20 12:22:27 am
Up
The Dark Shadow
User
Offline Off
Thanks for the tips, But user DC I have no idea how to do that

user DC has written:
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.


Code:
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)
29.04.20 02:51:40 am
Up
Cure Pikachu
User
Offline Off
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).
Code:
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
IMG:https://i.imgur.com/uUrye71.png
IMG:https://i.imgur.com/xpsyQRX.png
29.04.20 01:23:16 pm
Up
The Dark Shadow
User
Offline Off
Thanks everybody
29.04.20 06:02:57 pm
Up
DC
Admin
Offline Off
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.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
29.04.20 06:14:50 pm
Up
Mami Tomoe
User
Offline Off
user DC has written:
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.
29.04.20 06:23:21 pm
Up
DC
Admin
Offline Off
@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
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
To the start Previous 1 Next To the start