Skip to content

Commit

Permalink
Updates helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
spookydonut authored and Watermelon914 committed Dec 16, 2020
1 parent 1cfb920 commit 4693f2e
Show file tree
Hide file tree
Showing 31 changed files with 79 additions and 1,395 deletions.
9 changes: 9 additions & 0 deletions code/__DEFINES/__game.dm
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,12 @@ var/list/accessable_z_levels = list("1" = 10, "3" = 10, "4" = 10, "5" = 70)
else dist = (0.427*dx) + (0.934*dy)

return dist

//Update this whenever you need to take advantage of more recent byond features
#define MIN_COMPILER_VERSION 513
#define MIN_COMPILER_BUILD 1514
#if DM_VERSION < MIN_COMPILER_VERSION || DM_BUILD < MIN_COMPILER_BUILD
//Don't forget to update this part
#error Your version of BYOND is too out-of-date to compile this project. Go to https://secure.byond.com/download and update.
#error You need version 513.1514 or higher
#endif
9 changes: 8 additions & 1 deletion code/__DEFINES/misc.dm
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,11 @@
#define GUN_CATEGORY_SMG 2
#define GUN_CATEGORY_RIFLE 3
#define GUN_CATEGORY_SHOTGUN 4
#define GUN_CATEGORY_HEAVY 5
#define GUN_CATEGORY_HEAVY 5

/**
* Get the ultimate area of `A`, similarly to [get_turf].
*
* Use instead of `A.loc.loc`.
*/
#define get_area(A) (isarea(A) ? A : get_step(A, 0)?.loc)
30 changes: 0 additions & 30 deletions code/__HELPERS/#maths.dm
Original file line number Diff line number Diff line change
Expand Up @@ -70,36 +70,6 @@ var/list/sqrtTable = list(1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4,

// MATH PROCS

/proc/IsAboutEqual(a, b, deviation = 0.1)
return abs(a - b) <= deviation

// Performs a linear interpolation between a and b.
// Note that amount=0 returns a, amount=1 returns b, and
// amount=0.5 returns the mean of a and b.
/proc/Lerp(a, b, amount = 0.5)
return a + (b - a) * amount

/proc/Mean(...)
var/values = 0
var/sum = 0
for(var/val in args)
values++
sum += val
return sum / values

// The quadratic formula. Returns a list with the solutions, or an empty list
// if they are imaginary.
/proc/SolveQuadratic(a, b, c)
ASSERT(a)
. = list()
var/d = b*b - 4 * a * c
var/bottom = 2 * a
if(d < 0) return
var/root = sqrt(d)
. += (-b + root) / bottom
if(!d) return
. += (-b - root) / bottom

// Rotates a point around the given axis by a given amount of degrees
// You may want to round the result of this, it's very susceptible to floating point errors
/proc/RotateAroundAxis(var/point, var/axis, var/degrees)
Expand Down
173 changes: 5 additions & 168 deletions code/__HELPERS/game.dm
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
//This file was auto-corrected by findeclaration.exe on 25.5.2012 20:42:31

/proc/dopage(src,target)
var/href_list
var/href
href_list = params2list("src=\ref[src]&[target]=1")
href = "src=\ref[src];[target]=1"
src:temphtml = null
src:Topic(href, href_list)
return null

/proc/get_area(atom/A)
RETURN_TYPE(/area)
var/turf/T = get_turf(A)
if(T) . = T.loc

/proc/get_area_name(N) //get area by its name
for(var/area/A in all_areas)
if(A.name == N)
return A
return 0
/proc/get_area_name(atom/X, format_text = FALSE)
var/area/A = isarea(X) ? X : get_area(X)
if(!A)
return null
return format_text ? format_text(A.name) : A.name

/proc/in_range(source, user)
if(get_dist(source, user) <= 1)
Expand All @@ -39,65 +23,13 @@

return heard




//Magic constants obtained by using linear regression on right-angled triangles of sides 0<x<1, 0<y<1
//They should approximate pythagoras theorem well enough for our needs.
#define k1 0.934
#define k2 0.427
/proc/cheap_hypotenuse(Ax,Ay,Bx,By) // T is just the second atom to check distance to center with
var/dx = abs(Ax - Bx) //sides of right-angled triangle
var/dy = abs(Ay - By)
if(dx>=dy) return (k1*dx) + (k2*dy) //No sqrt or powers :)
else return (k2*dx) + (k1*dy)
#undef k1
#undef k2

// more efficient get_dist, doesn't sqrt

/proc/get_dist_sqrd(atom/Loc1 as turf|mob|obj, atom/Loc2 as turf|mob|obj)
var/dx = abs(Loc1.x - Loc2.x)
var/dy = abs(Loc1.y - Loc2.y)
return (dx * dx) + (dy * dy)

/proc/circlerange(center=usr,radius=3)

var/turf/centerturf = get_turf(center)
var/list/turfs = new/list()
var/rsq = radius * (radius+0.5)

for(var/atom/T in range(radius, centerturf))
var/dx = T.x - centerturf.x
var/dy = T.y - centerturf.y
if(dx*dx + dy*dy <= rsq)
turfs += T

//turfs += centerturf
return turfs

/proc/circleview(center=usr,radius=3)

var/turf/centerturf = get_turf(center)
var/list/atoms = new/list()
var/rsq = radius * (radius+0.5)

for(var/atom/A in view(radius, centerturf))
var/dx = A.x - centerturf.x
var/dy = A.y - centerturf.y
if(dx*dx + dy*dy <= rsq)
atoms += A

//turfs += centerturf
return atoms

/proc/trange(rad = 0, turf/centre = null) //alternative to range (ONLY processes turfs and thus less intensive)
if(!centre)
return

var/turf/x1y1 = locate(((centre.x - rad) < 1 ? 1 : centre.x - rad), ((centre.y-rad) < 1 ? 1 : centre.y - rad), centre.z)
var/turf/x2y2 = locate(((centre.x + rad) > world.maxx ? world.maxx : centre.x + rad), ((centre.y + rad) > world.maxy ? world.maxy : centre.y + rad), centre.z)
return block(x1y1, x2y2)

/proc/get_dist_euclidian(atom/Loc1 as turf|mob|obj,atom/Loc2 as turf|mob|obj)
var/dx = Loc1.x - Loc2.x
Expand All @@ -120,20 +52,6 @@
turfs += T
return turfs

/proc/circleviewturfs(center=usr,radius=3) //Is there even a diffrence between this proc and circlerangeturfs()?

var/turf/centerturf = get_turf(center)
var/list/turfs = new/list()
var/rsq = radius * (radius+0.5)

for(var/turf/T in view(radius, centerturf))
var/dx = T.x - centerturf.x
var/dy = T.y - centerturf.y
if(dx*dx + dy*dy <= rsq)
turfs += T
return turfs



//var/debug_mob = 0

Expand Down Expand Up @@ -331,87 +249,6 @@ proc/isInSight(var/atom/A, var/atom/B)

return candidates

/proc/ScreenText(obj/O, maptext="", screen_loc="CENTER-7,CENTER-7", maptext_height=480, maptext_width=480)
if(!isobj(O)) O = new /obj/screen/text()
O.maptext = maptext
O.maptext_height = maptext_height
O.maptext_width = maptext_width
O.screen_loc = screen_loc
return O

datum/projectile_data
var/src_x
var/src_y
var/time
var/distance
var/power_x
var/power_y
var/dest_x
var/dest_y

/datum/projectile_data/New(var/src_x, var/src_y, var/time, var/distance, \
var/power_x, var/power_y, var/dest_x, var/dest_y)
src.src_x = src_x
src.src_y = src_y
src.time = time
src.distance = distance
src.power_x = power_x
src.power_y = power_y
src.dest_x = dest_x
src.dest_y = dest_y

/proc/projectile_trajectory(var/src_x, var/src_y, var/rotation, var/angle, var/power)

// returns the destination (Vx,y) that a projectile shot at [src_x], [src_y], with an angle of [angle],
// rotated at [rotation] and with the power of [power]
// Thanks to VistaPOWA for this function

var/power_x = power * cos(angle)
var/power_y = power * sin(angle)
var/time = 2* power_y / 10 //10 = g

var/distance = time * power_x

var/dest_x = src_x + distance*sin(rotation);
var/dest_y = src_y + distance*cos(rotation);

return new /datum/projectile_data(src_x, src_y, time, distance, power_x, power_y, dest_x, dest_y)

/proc/GetRedPart(const/hexa)
return hex2num(copytext(hexa,2,4))

/proc/GetGreenPart(const/hexa)
return hex2num(copytext(hexa,4,6))

/proc/GetBluePart(const/hexa)
return hex2num(copytext(hexa,6,8))

/proc/GetHexColors(const/hexa)
return list(
GetRedPart(hexa),
GetGreenPart(hexa),
GetBluePart(hexa)
)

/proc/MixColors(const/list/colors)
var/list/reds = list()
var/list/blues = list()
var/list/greens = list()
var/list/weights = list()

for (var/i in 1 to colors.len)
reds.Add(GetRedPart(colors[i]))
blues.Add(GetBluePart(colors[i]))
greens.Add(GetGreenPart(colors[i]))
weights.Add(1)

var/r = mixOneColor(weights, reds)
var/g = mixOneColor(weights, greens)
var/b = mixOneColor(weights, blues)
return rgb(r,g,b)



/proc/convert_k2c(var/temp)
return ((temp - T0C))

Expand Down
Loading

0 comments on commit 4693f2e

Please sign in to comment.