ID:1906219
 
(See the best response by Kaiochao.)
Problem description: Hello I was wondering how would I go about calculating the distance between 2 or more objects. Actually that's an extremely vague description of what I really want.

Okay lets assume there is this skill that has a range of 3 tiles of in front of you. Do I use get_dist or something?

    ?   <--- Attacker         and/or    ?   <--- Attacker
*** <--- Defenders *
*
*

^
|
Defenders


I was thinking somewhere along the lines of:

for(var/mob/m in get_dist(src, locate(m.x + 3, m.y, m.z)))
//To attack 3 mobs in a straight line.
for(var/mob/m in oview(3,src)) if(get_dir(src,m) == dir)
{Tab} m << "deadface"


Loop through any mobs within three tiles and if the direction from you to m is the same as your direction, carry out your business.

There are much better ways to do this, but this is the best example I'm willing to try and do on a phone.
get_dist() returns a number.

If you're trying to iterate through a set of detected hits, you first need to generate that set.

With these parameters:
var dist = 3 // detection range


Inefficient method:
for(var/mob/m in range(dist, src))
if(get_dir(src, m) == dir)
// hit m

Slightly better method, called raycasting, in tile movement:
var hits[0]
var obj/ray = new (loc)
for(var/n in 1 to dist)
step(ray, dir)
hits |= obounds(ray)
ray.loc = null
for(var/mob/m in hits)
// hit m

The above, in pixel movement:
var hits[0]
var obj/ray = new (loc)
ray.step_x = step_x
ray.step_y = step_y
for(var/n in 1 to dist)
step(ray, dir)
hits |= obounds(ray)
ray.loc = null
for(var/mob/m in hits)
// hit m

Tile-based, no ray, more efficient:
var hits[0], turf/t = loc
for(var/n in 1 to dist)
t = get_step(t, dir)
hits |= range(0, t)
for(var/mob/m in hits)
// hit m

Math-based, requires some constants to be defined:
// in your header file, compiled before everything else
// set according to your world.icon_size
#define TILE_WIDTH 32
#define TILE_HEIGHT 32
// position of src, ignoring bound_x/y, intended for bounds()
var px = 1 + step_x + (x-1)*TILE_WIDTH
var py = 1 + step_y + (y-1)*TILE_HEIGHT

// size of the detection box
var hitbox_x, hitbox_y, hitbox_width, hitbox_height

// only supports cardinal directions
switch(dir)
if(NORTH)
hitbox_width = TILE_WIDTH
hitbox_height = TILE_HEIGHT * dist
hitbox_y = bound_height

if(SOUTH)
hitbox_width = TILE_WIDTH
hitbox_height = TILE_HEIGHT * dist
hitbox_y = -hitbox_height

if(EAST)
hitbox_width = TILE_WIDTH * dist
hitbox_height = TILE_HEIGHT
hitbox_x = bound_width

if(WEST)
hitbox_width = TILE_WIDTH * dist
hitbox_height = TILE_HEIGHT
hitbox_x = -hitbox_width

var hits[] = bounds(px + hitbox_x, py + hitbox_y, hitbox_width, hitbox_height, z) - src
for(var/mob/m in hits)
// hit m
Thank you both for responding. I really appreciate it. I like the "Tile-based, no ray" method. It performs one of the functions that I have requested. Now how will I manipulate that same system to acquire targets standing directly in front of but in a horizontal line.

I.e:
 
? <-- Attacker
*** <-- Victims
In response to Luchasi
Best response
Following the "Tile-based, no ray" method, and introducing a new parameter:
var half_width = 1 // how many tiles to the side
var dist = 1 // how many tiles in front
// prepare our hit detections list
var hits[0]

// get the center of the line
var turf/center = loc
for(var/n in 1 to dist)
center = get_step(center, dir)
hits |= range(0, center)

// now spread out and detect hits
var turf/t1 = center, turf/t2 = center
var d1 = turn(dir, 90), d2 = turn(dir, -90)
for(var/n in 1 to half_width)
t1 = get_step(t1, d1)
t2 = get_step(t2, d2)
hits |= range(0, t1)
hits |= range(0, t2)

for(var/mob/m in hits)
// hit m

It won't detect anything closer than the line, not sure if that's a problem.
Thank you so much. Cheers to the both you ^^