Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added Graph module to Math #3404

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

feat: added Graph module to Math #3404

wants to merge 13 commits into from

Conversation

jyoung4242
Copy link
Contributor

Open to feedback on the API here.

Features added, Graphs, Nodes, Edges, Vertices...
Pathfinding and graph traversal functions
QOL methods for using graphs...

Special reqeust for feedback on the Astar vs Dijkstra's pathfinding.... the API is subtle different where Astar uses spatial PositionNodes and Dijkstra's uses weighted edges.

Open for ideas on this.

3 Files added

  • Graph.ts in /Math
  • GraphSpec.ts in /src/spec
  • 07-graph.mdx in /docs

Comment on lines 796 to 800
const generatedUuid = uuid.replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});

Check failure

Code scanning / CodeQL

Insecure randomness High

This uses a cryptographically insecure random number generated at Math.random() in a security context.

Copilot Autofix

AI 5 days ago

To fix the problem, we need to replace the use of Math.random() with a cryptographically secure random number generator. In a Node.js environment, we can use the crypto module's randomBytes function to generate secure random values. We will modify the generateUUID method to use crypto.randomBytes instead of Math.random().

Suggested changeset 1
src/engine/Math/Graph.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/engine/Math/Graph.ts b/src/engine/Math/Graph.ts
--- a/src/engine/Math/Graph.ts
+++ b/src/engine/Math/Graph.ts
@@ -1,2 +1,3 @@
 import { Vector } from './vector';
+import { randomBytes } from 'crypto';
 
@@ -796,3 +797,3 @@
     const generatedUuid = uuid.replace(/[xy]/g, function (c) {
-      const r = (Math.random() * 16) | 0;
+      const r = randomBytes(1)[0] % 16;
       const v = c === 'x' ? r : (r & 0x3) | 0x8;
EOF
@@ -1,2 +1,3 @@
import { Vector } from './vector';
import { randomBytes } from 'crypto';

@@ -796,3 +797,3 @@
const generatedUuid = uuid.replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0;
const r = randomBytes(1)[0] % 16;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
Copilot is powered by AI and may make mistakes. Always verify output.
Unable to commit as this autofix suggestion is now outdated
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replacing Math.random() wth Ex' Random class

Copy link

cloudflare-workers-and-pages bot commented Apr 2, 2025

Deploying excaliburjs with  Cloudflare Pages  Cloudflare Pages

Latest commit: e82555a
Status: ✅  Deploy successful!
Preview URL: https://2dda6921.excaliburjs.pages.dev
Branch Preview URL: https://graph.excaliburjs.pages.dev

View logs

Copy link
Member

@eonarheim eonarheim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks really awesome, just a couple notes.

Question, do we want to deprecate the pathfinding plugin since we are moving this functionality into core? https://github.com/excaliburjs/excalibur-pathfinding/

@@ -5,6 +5,79 @@
"requires": true,
"packages": {
"": {
"name": "excalibur",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this file will need to be reverted too, makes reference to your local Meet/Excalibur

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might need some guidance on this, i've never touched a package-lock.json

it's usually managed by node, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've deleted my package-lock and redid a npm install and it keeps coming up with local references in my package-lock...
@eonarheim

Edges connect nodes and can have properties:

weight: Numeric value representing distance or cost (default: 0)
directed: Whether the edge is one-way or bidirectional (default: true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious if directed should default to false? No strong data, but I generally use bidirectional edges when I'm building nav mesh type situations.

Also is the direction A->B in .addEdge(nodeA, nodeB, ...)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changing default is easy, and i can do some parameter renaming to accommodate direction

* Alias for a node in a graph, representing a vertex in a geometric context.
* @template T The type of data stored in this vertex.
*/
export type Vertex<T> = Node<T>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we stick to either node or vertex? I'm a little worried the synonym could be confusing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems there are duplicate methods with different names

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would actually prefer to keep it to just nodes, and remove vertex

@jyoung4242
Copy link
Contributor Author

@eonarheim i'm considering modifying how the positions work, and use it to dictate edge weight
that way if you set weights on an edge OR position, astar will still work

@jyoung4242
Copy link
Contributor Author

@eonarheim we should deprecate the pathfinding plugin in, but I would think we should recreate the sample application using the core native Graph library before deprecating

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants