Skip to content

Commit

Permalink
added vector library and switched to startest
Browse files Browse the repository at this point in the history
  • Loading branch information
Acepie committed Apr 21, 2024
1 parent 69c323f commit 47766e0
Show file tree
Hide file tree
Showing 6 changed files with 332 additions and 80 deletions.
5 changes: 3 additions & 2 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ target = "javascript"
# https://gleam.run/writing-gleam/gleam-toml/.

[dependencies]
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
gleam_stdlib = ">= 0.36.0 and < 2.0.0"
p5js_gleam = ">= 1.0.1 and < 2.0.0"
prng = ">= 3.0.2 and < 4.0.0"
gleam_community_maths = ">= 1.1.0 and < 2.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
esgleam = ">= 0.6.0 and < 1.0.0"
startest = ">= 0.2.0 and < 1.0.0"
23 changes: 19 additions & 4 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,34 @@
# You typically do not need to edit this file

packages = [
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
{ name = "birl", version = "1.6.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "976CFF85D34D50F7775896615A71745FBE0C325E50399787088F941B539A0497" },
{ name = "esgleam", version = "0.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "simplifile"], otp_app = "esgleam", source = "hex", outer_checksum = "571D149ACFB7C7A81CF8C7A70ECFDA5A084ADBF706962353C0DA85787CB4DD24" },
{ name = "exception", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "F5580D584F16A20B7FCDCABF9E9BE9A2C1F6AC4F9176FA6DD0B63E3B20D450AA" },
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" },
{ name = "gleam_bitwise", version = "1.3.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_bitwise", source = "hex", outer_checksum = "B36E1D3188D7F594C7FD4F43D0D2CE17561DE896202017548578B16FE1FE9EFC" },
{ name = "gleam_stdlib", version = "0.37.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "5398BD6C2ABA17338F676F42F404B9B7BABE1C8DC7380031ACB05BBE1BCF3742" },
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" },
{ name = "gleam_community_ansi", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "FE79E08BF97009729259B6357EC058315B6FBB916FAD1C2FF9355115FEB0D3A4" },
{ name = "gleam_community_colour", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "795964217EBEDB3DA656F5EB8F67D7AD22872EB95182042D3E7AFEF32D3FD2FE" },
{ name = "gleam_community_maths", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_community_maths", source = "hex", outer_checksum = "E30C61A75051DAF7CFD77C4FBAA04140FDA0B5D831955E7A74521E5576E2780D" },
{ name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" },
{ name = "gleam_javascript", version = "0.8.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_javascript", source = "hex", outer_checksum = "14D5B7E1A70681E0776BF0A0357F575B822167960C844D3D3FA114D3A75F05A8" },
{ name = "gleam_json", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "8B197DD5D578EA6AC2C0D4BDC634C71A5BCA8E7DB5F47091C263ECB411A60DF3" },
{ name = "gleam_stdlib", version = "0.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" },
{ name = "glint", version = "0.18.0", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_community_colour", "gleam_stdlib", "snag"], otp_app = "glint", source = "hex", outer_checksum = "BB0F14643CC51C069A5DC6E9082EAFCD9967AFD1C9CC408803D1A40A3FD43B54" },
{ name = "p5js_gleam", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "p5js_gleam", source = "hex", outer_checksum = "5DCFACE980EDE238BC81B8837C1A8DBE24B277B54B004B7BBCC6B8F473A9BDAD" },
{ name = "prng", version = "3.0.2", build_tools = ["gleam"], requirements = ["gleam_bitwise", "gleam_stdlib"], otp_app = "prng", source = "hex", outer_checksum = "C61B103F9AF5031ADAA35187CCE7130845EF5088D88FD084E5995D4FBEC9D745" },
{ name = "ranger", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "1566C272B1D141B3BBA38B25CB761EF56E312E79EC0E2DFD4D3C19FB0CC1F98C" },
{ name = "simplifile", version = "1.7.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "1D5DFA3A2F9319EC85825F6ED88B8E449F381B0D55A62F5E61424E748E7DDEB0" },
{ name = "snag", version = "0.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "snag", source = "hex", outer_checksum = "54D32E16E33655346AA3E66CBA7E191DE0A8793D2C05284E3EFB90AD2CE92BCC" },
{ name = "startest", version = "0.2.0", build_tools = ["gleam"], requirements = ["argv", "birl", "exception", "gleam_community_ansi", "gleam_erlang", "gleam_javascript", "gleam_stdlib", "glint", "simplifile", "tom"], otp_app = "startest", source = "hex", outer_checksum = "89F8575C91696D1713075F0B850A54160A32580A7CF415E045A0EEAD498AA30D" },
{ name = "thoas", version = "0.4.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "4918D50026C073C4AB1388437132C77A6F6F7C8AC43C60C13758CC0ADCE2134E" },
{ name = "tom", version = "0.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "tom", source = "hex", outer_checksum = "0831C73E45405A2153091226BF98FB485ED16376988602CC01A5FD086B82D577" },
]

[requirements]
esgleam = { version = ">= 0.6.0 and < 1.0.0" }
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
gleam_community_maths = { version = ">= 1.1.0 and < 2.0.0" }
gleam_stdlib = { version = ">= 0.36.0 and < 2.0.0" }
p5js_gleam = { version = ">= 1.0.1 and < 2.0.0" }
prng = { version = ">= 3.0.2 and < 4.0.0" }
startest = { version = ">= 0.2.0 and < 1.0.0" }
93 changes: 93 additions & 0 deletions src/vector.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import gleam/float
import gleam_community/maths/elementary

/// Represents a 3D Vector
pub type Vector {
Vector(x: Float, y: Float, z: Float)
}

/// Adds 2 vectors together
pub fn vector_add(v1: Vector, v2: Vector) -> Vector {
Vector(v1.x +. v2.x, v1.y +. v2.y, v1.z +. v2.z)
}

/// Subtracts the second vector from the first vector
pub fn vector_subtract(v1: Vector, v2: Vector) -> Vector {
Vector(v1.x -. v2.x, v1.y -. v2.y, v1.z -. v2.z)
}

/// Computes the dot product of 2 vectors
pub fn vector_dot(v1: Vector, v2: Vector) -> Float {
v1.x *. v2.x +. v1.y *. v2.y +. v1.z *. v2.z
}

/// Computes the cross product of 2 vectors
pub fn vector_cross(v1: Vector, v2: Vector) -> Vector {
let yz = v1.y *. v2.z -. v1.z *. v2.y
let zx = v1.z *. v2.x -. v1.x *. v2.z
let xy = v1.x *. v2.y -. v1.y *. v2.x
Vector(yz, zx, xy)
}

/// Multiplies the vector by some value
pub fn vector_multiply(v: Vector, mult: Float) -> Vector {
Vector(v.x *. mult, v.y *. mult, v.z *. mult)
}

/// Divides the vector by some value
pub fn vector_divide(v: Vector, mult: Float) -> Vector {
Vector(v.x /. mult, v.y /. mult, v.z /. mult)
}

/// Computes the square of the magnitude of the vector
/// Mainly used for performance when computing square roots is unnecessary
pub fn vector_magnitude_squared(v: Vector) -> Float {
v.x *. v.x +. v.y *. v.y +. v.z *. v.z
}

/// Computes the magnitude of the vector
pub fn vector_magnitude(v: Vector) -> Float {
// magnitude square is always positive
let assert Ok(mag) = float.square_root(vector_magnitude_squared(v))
mag
}

/// Computes a vector of the same heading with a magnitude of 1
pub fn vector_normalize(v: Vector) -> Vector {
vector_divide(v, vector_magnitude(v))
}

/// Computes a vector of the same heading with maximum magnitude
pub fn vector_limit(v: Vector, limit: Float) -> Vector {
let lim_squared = limit *. limit
case vector_magnitude_squared(v) {
m if m <=. lim_squared -> v
_ -> vector_multiply(vector_normalize(v), limit)
}
}

/// Computes the distance between 2 vectors
pub fn vector_distance(v1: Vector, v2: Vector) -> Float {
vector_magnitude(vector_subtract(v1, v2))
}

/// Creates a 2d version of the vector
pub fn vector_2d(v: Vector) -> Vector {
Vector(v.x, v.y, 0.0)
}

/// Computes the heading in radians of a vector
pub fn vector_heading2d(v: Vector) -> Float {
let res = elementary.atan2(v.y, v.x)
case res {
r if r <. 0.0 -> r +. 2.0 *. elementary.pi()
_ -> res
}
}

/// Rotates a vector around the z axis by the given amount in radians
pub fn vector_rotate2d(v: Vector, rotation: Float) -> Vector {
let heading = vector_heading2d(v) +. rotation
let mag = vector_magnitude(vector_2d(v))
Vector(elementary.cos(heading) *. mag, elementary.sin(heading) *. mag, v.z)
}
76 changes: 2 additions & 74 deletions test/bullet_heck_gleam_test.gleam
Original file line number Diff line number Diff line change
@@ -1,78 +1,6 @@
import dungeon
import gleam/dict
import gleam/iterator.{repeatedly, take}
import gleam/list
import gleeunit
import gleeunit/should
import room
import startest

pub fn main() {
gleeunit.main()
startest.run(startest.default_config())
}

pub fn validate_room_is_traversable(d: dungeon.Dungeon) {
let rooms =
d.rooms
|> dict.to_list()

use #(#(column, row), room) <- list.each(rooms)

// Check direction is symmetrical
// Returns if the direction is navigable
let test_direction = fn(dir: room.Direction) -> Bool {
case room.is_navigable(room, dir) {
// Confirm the other room can navigate to this room
True -> {
let next_room_coords = dungeon.next_room_indices(column, row, dir)
let next_room = dict.get(d.rooms, next_room_coords)
should.be_ok(next_room)
let assert Ok(next_room) = next_room
should.be_true(room.is_navigable(next_room, room.inverse_direction(dir)))
True
}
// Confirm the other room can't navigate to this room or is empty
False -> {
let next_room_coords = dungeon.next_room_indices(column, row, dir)
let next_room = dict.get(d.rooms, next_room_coords)

case next_room {
Error(_) -> Nil
Ok(next_room) -> {
should.be_false(room.is_navigable(
next_room,
room.inverse_direction(dir),
))
}
}
False
}
}
}

// Check each room has at least 1 other navigable room
should.be_true(
list.any(
[
test_direction(room.Left),
test_direction(room.Right),
test_direction(room.Top),
test_direction(room.Bottom),
test_direction(room.TopLeft),
test_direction(room.TopRight),
test_direction(room.BottomLeft),
test_direction(room.BottomRight),
],
fn(x) { x },
),
)
}

pub fn validate_generation_test() {
{
use <- repeatedly

dungeon.generate_dungeon()
|> validate_room_is_traversable
}
|> take(30)
}
83 changes: 83 additions & 0 deletions test/dungeon_test.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import dungeon
import gleam/dict
import gleam/iterator.{repeatedly, take}
import gleam/list
import room
import startest.{describe, it}
import startest/expect

pub fn validate_room_is_traversable(d: dungeon.Dungeon) {
let rooms =
d.rooms
|> dict.to_list()

use #(#(column, row), room) <- list.each(rooms)

// Check direction is symmetrical
// Returns if the direction is navigable
let test_direction = fn(dir: room.Direction) -> Bool {
case room.is_navigable(room, dir) {
// Confirm the other room can navigate to this room
True -> {
let next_room_coords = dungeon.next_room_indices(column, row, dir)
let next_room = dict.get(d.rooms, next_room_coords)
expect.to_be_ok(next_room)
let assert Ok(next_room) = next_room
expect.to_be_true(room.is_navigable(
next_room,
room.inverse_direction(dir),
))
True
}
// Confirm the other room can't navigate to this room or is empty
False -> {
let next_room_coords = dungeon.next_room_indices(column, row, dir)
let next_room = dict.get(d.rooms, next_room_coords)

case next_room {
Error(_) -> Nil
Ok(next_room) -> {
expect.to_be_false(room.is_navigable(
next_room,
room.inverse_direction(dir),
))
}
}
False
}
}
}

// Check each room has at least 1 other navigable room
expect.to_be_true(
list.any(
[
test_direction(room.Left),
test_direction(room.Right),
test_direction(room.Top),
test_direction(room.Bottom),
test_direction(room.TopLeft),
test_direction(room.TopRight),
test_direction(room.BottomLeft),
test_direction(room.BottomRight),
],
fn(x) { x },
),
)
}

pub fn validate_generation_tests() {
describe("dungeon", [
it("generated dungeons should always be traversable", fn() {
{
use <- repeatedly

dungeon.generate_dungeon()
|> validate_room_is_traversable
}
|> take(30)

Nil
}),
])
}
Loading

0 comments on commit 47766e0

Please sign in to comment.