ID:1996110
 
(See the best response by Fenoopy.)
Code:
client
var
startPx = 0
endPx = 0
startPy = 0
endPy = 0

MouseMove(object,location,control,params)
var/list/_params = params2list(params)
endPx = screenLocToPx(_params["screen-loc"])[1]
endPy = screenLocToPx(_params["screen-loc"])[2]

MouseDrag(src_object,over_object,src_location,over_location,src_control,over_control,params)
var/list/_params = params2list(params)
startPx = screenLocToPx(_params["screen-loc"])[1]
startPy = screenLocToPx(_params["screen-loc"])[2]

MouseDrop()
if(startPx && startPy)
var/pxDiff = startPx - endPx
var/pyDiff = startPy - endPx
for(var/robot/R in bounds(locate(1,1,1),startPx,startPy,pxDiff,pyDiff))
R.influence = mob
startPx = 0
startPy = 0


Problem description:
Something is off with my math or something, because what this is supposed to do is let me click+drag to highlight an area, and any robots in that area get added to my influence, but for some reason, my selections produce... strange... results, and I really can't figure out why. If I change the for() to set turfs to black, this is the result, and there doesn't seem to be any real consistency.



Sometimes it's near my mouse, sometimes it seems offset by a specific amount, sometimes it's to the left.

I just don't know anymore, all I want to do is make tiny robot based RTS :(
Whilst your example is a little off, that is indeed one way of doing it. Instead of checking mobs in the bounds, check mobs in the world, compare their cx() and cy() and if they're within start/end values, then set influence.

However that's unfortunately not going to be very scalable for my game, since I'm hoping to have hundreds if not thousands of bots running around at any given time, and looping every single one for every single mouse drag operation isn't going to feasible.
Best response
Have you set world << output to output the coordinates? Maybe the params proc isn't working correctly

var/pyDiff = startPy - endPx

nvm this is the bug u stuck a px on the py, i am tired and didnt see it lmfao
I have, and it seems to be.
Haha, good catch, I've ammended that, but I'm still having the issue, though less strangely and maybe slightly more predictable?



Before you mentioned it, I tried switching startPx/y and endPx/y, but for some reason that returns inverted results. Dragging from bottom left to top right results in a negative px/yDiff
I mean, you can see the source if you really want but there isn't much to it. Here's all the relevant code, lol.

robot
parent_type = /mob

proc
screenLocToPx(var/S)
var/list/screenLoc = parseScreenLoc(S)
return list((screenLoc[1]*world.icon_size)+screenLoc[2],(screenLoc[3]*world.icon_size)+screenLoc[4])

parseScreenLoc(var/S)
if(!isnum(text2num(S)))
S = copytext(S, findtext(S, ":")+1)
var/x = text2num(S)
S = copytext(S, length("[x]")+2)
var/px = text2num(S)
S = copytext(S, length("[px]")+2)
var/y = text2num(S)
S = copytext(S, length("[y]")+2)
var/py = text2num(S)
return list(x,px,y,py)


That's pretty much it.
Fenoopy wrote:
var/pxDiff = startPx - endPx
var/pyDiff = startPy - endPy


to
var/pxDiff = endPx - startPx
var/pyDiff = endPy - startPy

i think


Rushnut wrote:
Before you mention it, I tried switching startPx/y and endPx/y, but for some reason that returns inverted results. Dragging from bottom left to top right results in a negative px/yDiff
world<<"Started from [startPx],[startPy] finished at [endPx],[endPy]. Size [pxDiff],[pyDiff]"


Dragging from bottom left to top right resulted in this:

Started from 559,505 finished at 403,391. Size 156,114

I have no idea why it's inverted, but it is.

No, that doesn't work, it's supposed to start from the bottom left. I'm offsetting from the bottom left tile a number of pixels equal to the distance (in pixels) the mouse is, then drawing a box equal to the size (in pixels) that I dragged my mouse.

Though it might need a -32 now that I think about it, but that probably won't fix my issue.
Hrm, I threw in
for(var/robot/R in bounds(locate(1,1,1),startPx-32,startPy-32,pxDiff-32,pyDiff-32))


And now I get even MORE accurate, but still not accurate enough, results, something is still off and I'm not sure what.
Alright so I figured a few things wrong.

1: I figured out why it was inverting
2: It's because MouseDrag() was re-setting every tick
3: Figuring that out meant I could switch back to MouseDrop() instead of MouseMove()
4: That means it no longer inverted
5: It's still broken though.

Here's what it looks like now:

client
var
startPx = 0
endPx = 0
startPy = 0
endPy = 0

MouseMove(object,location,control,params)

MouseDrag(src_object,over_object,src_location,over_location,src_control,over_control,params)
if(!startPx && !startPy)
var/list/_params = params2list(params)
startPx = screenLocToPx(_params["screen-loc"])[1]
startPy = screenLocToPx(_params["screen-loc"])[2]

MouseDrop(src_object,over_object,src_location,over_location,src_control,over_control,params)
if(startPx && startPy)
var/list/_params = params2list(params)
endPx = screenLocToPx(_params["screen-loc"])[1]
endPy = screenLocToPx(_params["screen-loc"])[2]
var/pxDiff = endPx - startPx
var/pyDiff = endPy - startPy
world<<"Started from [startPx],[startPy] finished at [endPx],[endPy]. Size [pxDiff],[pyDiff]"
for(var/turf/R in bounds(locate(1,1,1),startPx-32,startPy-32,pxDiff-32,pyDiff-32))
R.color = rgb(0,0,0)
startPx = 0
startPy = 0
Deeper into the rabbit hole, adding world<<_params["screen-loc"] in MouseDrag, and it says when dragging from tile 1,1, that my mouse is in the region of 10:5,10:5

lolwut
Found it! I was using screen-loc for some reason. The fix was hella wordy though

client
var
startPx = 0
startPy = 0

MouseMove(object,location,control,params)

MouseDrag(src_object,over_object,src_location,over_location,src_control,over_control,params)
if((!startPx && !startPy) && istype(src_location,/turf))
var/list/_params = params2list(params)
startPx = screenLocToPx("[src_location:x-1]:[parseScreenLoc(_params["screen-loc"])[2]],[src_location:y-1]:[parseScreenLoc(_params["screen-loc"])[4]]")[1]
startPy = screenLocToPx("[src_location:x-1]:[parseScreenLoc(_params["screen-loc"])[2]],[src_location:y-1]:[parseScreenLoc(_params["screen-loc"])[4]]")[2]

MouseDrop(src_object,over_object,src_location,over_location,src_control,over_control,params)
if(startPx && startPy)
var/list/_params = params2list(params)
var/endPx = screenLocToPx("[over_location:x-1]:[parseScreenLoc(_params["screen-loc"])[2]],[over_location:y-1]:[parseScreenLoc(_params["screen-loc"])[4]]")[1]
var/endPy = screenLocToPx("[over_location:x-1]:[parseScreenLoc(_params["screen-loc"])[2]],[over_location:y-1]:[parseScreenLoc(_params["screen-loc"])[4]]")[2]
if(endPx && endPy)
var/pxDiff = endPx - startPx
var/pyDiff = endPy - startPy
for(var/robot/R in bounds(locate(1,1,1),startPx,startPy,pxDiff,pyDiff))
R.influence = mob
startPx = 0
startPy = 0