ID:1096187
 
Keywords: get_bounds
I was recently working on a project, converting the tile movement into pixel movement and realized there wasn't an equivalent for get_step() to use for pixel movement.

I looked through the forums and found a post about it in Developer Help (although I cannot find it now.) and it helped me to create a workaround. I thought I would share this workaround with you.

proc/get_bounds(mob/M, var/boundz_x=0, var/boundz_y=0)
if(M.dir == EAST)
boundz_x = M.bound_width
else if(M.dir == WEST)
boundz_x = -M.bound_width

else if(M.dir == NORTH)
boundz_y = M.bound_height
else if(M.dir == SOUTH)
boundz_y = -M.bound_height

return obounds(M, boundz_x, boundz_y)


Here is where you can see an example of this proc being utilized (for reference purposes.):

mob/verb
Punch()
flick("punch", usr)
for(var/mob/M in get_bounds(usr))
M.health = 0
M << "<b>You dead!"
M.Die()


If anyone can find the post I used to create this procedure, please share. I'd like to reference it!




[Edit]

Since get_bounds() returns obounds(), it searches for a mob within the bounds you are facing.

Example: If your character is facing EAST, it searches for a mob within your bounds_width infront of you. It then returns obounds(M, M.bounds_width, 0). The 0 within the args makes sure it doesn't search any pixels higher or lower than your mob's current location within the tile. The same is said for searching for a mob when you are facing either NORTH or SOUTH.

[/Edit]
Thank you! This was the only thing keeping me from using pixel movement.
You're welcome. I'm glad someone found it useful. ;)
It has been a while, so I can't remember for sure, but I am pretty sure I was looking for something like this when I was working on Captive for a contest.

I'm not 100% sure when, or if I would need it again, but either way; great work and thanks for making it. I think this will help a lot of people who are completely new to pixel movement like I was with Captive.
I think it's mostly a matter of comparing tile-based movement to pixel movement. This is instantly available for tile-based movement, so it makes sense to have it for pixel movement; even if it could be handled relatively easily with the reference or other things.

It's just slightly easier, and probably helps new people a good bit.
Right. There's probably a million workarounds, but this is one that I came up with.
Galactic Soldier wrote:
I'm amazed how anybody would find any help from this. All you'd have to do is look at the reference; it's not hard to figure out for yourself.

You're amazed pretty easily.

This is neat, but it doesn't really cover diagonals. Here's how I do it.

proc
get_bounds(mob/m)
var offset_x, offset_y
if(m.dir & NORTH) offset_y += m.bounds_height
if(m.dir & SOUTH) offset_y -= m.bounds_height
if(m.dir & EAST) offset_x += m.bounds_width
if(m.dir & WEST) offset_x -= m.bounds_width
return obounds(m, offset_x, offset_y)


Although I usually use step_size instead of bounds, I see why you use bounds.

Uh... this forum is supposed to be to teach people how to do things. If they're new, how are they going to read your slop of code.

I would never program like that, because it sucks for management. Ternary conditions aren't anything new, but they aren't popular because they're hard to read in comparison.
Here's mine!
atom/movable/proc/get_bounds(dir)
dir = dir || src.dir
var dh = dir & 12
var dv = dir & 3
return obounds(src,
dh && (dh == 4 ? bound_width : -bound_width),
dv && (dv == 1 ? bound_height : -bound_height))
It's not much of a difference. In yours, you're looping through mobs around you that you can't hit. In mine, you're getting a list of more than just mobs, but all the mobs in the returned list will be punched.
In response to Kaiochao
Kaiochao wrote:
Here's mine!
> atom/movable/proc/get_bounds(dir)
> dir = dir || src.dir
> var dh = dir & 12
> var dv = dir & 3
> return obounds(
> dh && (dh == 4 ? bound_width : -bound_width),
> dv && (dv == 1 ? bound_height : -bound_height))
>



Kaio would you mind giving me a commented version of this? Basically broken down. It seems more effiecent than mine, but im unsure exactly how it works even going over it a few times.

mob/proc
front(d)
// d = distance in pixels
var x = 0
var y = 0
if(dir & NORTH)
y += d
if(dir & SOUTH)
y -= d
if(dir & EAST)
x += d
if(dir & WEST)
x -= d
return obounds(src, x, y)

I wouldn't really say I resent ternary operators, I just prefer to lay things out in a clean way, which can be quickly understood. When things start getting large, it sucks to sift through a bunch of stuff you can't read easily, that being the reason I prefer the way I posted.

I don't exactly find them 'hard' to read, just less readable than if().

After reading Kaiochao's snippet, thats a pretty neat way of going about it. What exactly are the values for the direction macros? I've never really gotten around to learning them.
In response to D-Cire
Mine:
atom/movable/proc/get_bounds(dir)
// Set 'dir' to 'dir', or 'src.dir' if 'dir' is false.
dir = dir || src.dir

// Get the horizontal component of 'dir'.
var dh = dir & 12

// Get the vertical component of 'dir'.
var dv = dir & 3

return obounds(src,
// If there is no horizontal component, this is 0.
// Otherwise, dh is either 4 or 8 (because they make 12)
dh && (dh == 4 ? bound_width : -bound_width),

// Same as above but vertical; dv is either 0, 1, or 2.
dv && (dv == 1 ? bound_height : -bound_height))


Yours:
mob/proc
front(d)
// d = distance in pixels
var x = 0
var y = 0

// If NORTH is a part of 'dir', add 'd' to the extra height
if(dir & NORTH)
y += d

// etc.
if(dir & SOUTH)
y -= d
if(dir & EAST)
x += d
if(dir & WEST)
x -= d
return obounds(src, x, y)
we all know yut put has a fetish for saying the esoteric word "esoteric" every chance he gets.
In response to Kitsueki
1 = NORTH
2 = SOUTH
4 = EAST
8 = WEST

1 | 4 = 5 = NORTHEAST
1 | 8 = 9 = NORTHWEST
2 | 4 = 6 = SOUTHEAST
2 | 8 = 10 = SOUTHWEST

1 | 2 = 3 = "vertical", or NORTHSOUTH, if that was a thing
4 | 8 = 12 = "horizontal"
Thanks, I somewhat understand that better. The numerical values for the directions has always thrown me off. Never really learned them.
In response to Kaiochao
Thanks Kaiochao.
This is great. I'm enjoying seeing all of the other ways you guys handle this.
Galactic Soldier wrote:
I'm enjoying seeing people struggle with understanding basic logical and bitwise operators.

Pfft, not my fault i completely skipped that area of programming. Lmao.