Skip to content

Conversation

@mpragnay
Copy link

  • Removed hardcoded offsets

  • Code Cleanup

Copilot AI review requested due to automatic review settings January 17, 2026 23:50
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors hardcoded offset arrays by replacing them with dynamically generated grids. The hardcoded 5x5 collision offset grid (25 elements) and 15x15 z-coordinate offset grid (225 elements) are replaced with two defines (COLLISION_RANGE and Z_RANGE) and a reusable generate_offsets function that creates these grids at runtime during initialization.

Changes:

  • Added COLLISION_RANGE and Z_RANGE defines to specify grid dimensions
  • Created a generate_offsets function to dynamically generate offset grids
  • Removed 280+ lines of hardcoded offset array definitions
  • Initialized offset arrays in the init function using the new generator

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 123 to 135
void generate_offsets(int offsets[][2], int offset_range) {
int half_grid = offset_range / 2;
int left_most = -half_grid;
int right_most = offset_range + left_most - 1;
int index = 0;
for (int dy = left_most; dy <= right_most; dy++) {
for (int dx = left_most; dx <= right_most; dx++) {
offsets[index][0] = dx;
offsets[index][1] = dy;
index++;
}
}
}
Copy link

Copilot AI Jan 17, 2026

Choose a reason for hiding this comment

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

This function definition in a header file will cause multiple definition linker errors when the header is included in multiple translation units (which it is - in binding.c and through drivenet.h). The function should either be declared as static inline or moved to a .c file with only a declaration in the header.

Copilot uses AI. Check for mistakes.
Comment on lines 138 to 139
int collision_offsets[25][2] = {0};
int z_offsets[225][2] = {0};
Copy link

Copilot AI Jan 17, 2026

Choose a reason for hiding this comment

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

These non-const, non-static global variable definitions in a header file will cause multiple definition linker errors when the header is included in multiple translation units (binding.c and drivenet.h). These should either be declared as extern in the header with definitions in a .c file, or declared as static to have file scope in each translation unit (though the latter would create separate copies).

Copilot uses AI. Check for mistakes.
Comment on lines +103 to +104
// Offsets
#define COLLISION_RANGE 5
Copy link

Copilot AI Jan 17, 2026

Choose a reason for hiding this comment

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

The new defines COLLISION_RANGE and Z_RANGE lack documentation explaining what they represent. Consider adding comments explaining that these define the grid dimensions for collision detection and z-coordinate updates, respectively. For example: "Grid dimension for collision detection (creates a 5x5 grid)" and "Grid dimension for z-coordinate updates (creates a 15x15 grid)".

Suggested change
// Offsets
#define COLLISION_RANGE 5
// Offsets
// Grid dimension for collision detection (creates a 5x5 grid)
#define COLLISION_RANGE 5
// Grid dimension for z-coordinate updates (creates a 15x15 grid)

Copilot uses AI. Check for mistakes.
{1, -1}, // bottom-right
{-1, -1} // bottom-left
};

Copy link

Copilot AI Jan 17, 2026

Choose a reason for hiding this comment

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

The generate_offsets function lacks documentation explaining its purpose, parameters, and behavior. Consider adding a comment block above the function explaining that it generates a square grid of 2D offsets centered at (0,0), the offset_range parameter determines the grid dimensions (e.g., 5 creates a 5x5 grid from -2 to 2), and the offsets array must be pre-allocated to size offset_range^2.

Suggested change
/**
* Generates a square grid of 2D integer offsets centered at (0,0).
*
* The resulting grid has dimensions offset_range x offset_range. For example,
* an offset_range of 5 will generate offsets in the inclusive range [-2, 2]
* for both x and y, producing 25 (dx, dy) pairs.
*
* Parameters:
* offsets Pre-allocated array of size offset_range^2 by 2 that will
* be filled with the generated (dx, dy) offset pairs.
* offset_range Side length of the square grid. Typically a positive odd
* integer so that the grid is symmetrically centered on (0,0).
*/

Copilot uses AI. Check for mistakes.
@greptile-apps
Copy link

greptile-apps bot commented Jan 17, 2026

Greptile Summary

Replaced 250 lines of hardcoded collision and z-offset arrays with a programmatic generate_offsets() function that dynamically generates grid patterns at runtime.

Key Changes:

  • Removed static arrays collision_offsets[25][2] and z_offsets[225][2] containing hardcoded coordinate pairs
  • Added generate_offsets() function to programmatically generate offset grids based on range parameters
  • Introduced COLLISION_RANGE (5) and Z_RANGE (15) constants for configurability
  • Offset generation called in init() function during system initialization

Critical Issues:

  • Non-static global variable definitions in header will cause multiple definition linker errors across compilation units (binding.c, drive.c, visualize.c)
  • Function definition in header will also cause multiple definition errors without static inline or proper declaration/definition separation
  • These are blocking issues that will prevent successful compilation and linking

Confidence Score: 1/5

  • This PR contains critical linker errors that will prevent successful compilation
  • The code introduces non-static global variables and a function definition in a header file that is included by multiple compilation units, causing guaranteed multiple definition linker errors. While the offset generation logic is correct and the cleanup is beneficial, the implementation fundamentally breaks the build
  • pufferlib/ocean/drive/drive.h requires immediate fixes for linker errors before merge

Important Files Changed

Filename Overview
pufferlib/ocean/drive/drive.h Replaced hardcoded offset arrays with programmatic generation, but introduced linker errors with non-static global variables and function definition in header

Sequence Diagram

sequenceDiagram
    participant Init as init()
    participant GenOff as generate_offsets()
    participant CollArr as collision_offsets[25][2]
    participant ZArr as z_offsets[225][2]
    participant Compute as compute_agent_metrics()
    participant Step as step()
    participant CheckN as checkNeighbors()
    
    Note over Init: System Initialization
    Init->>GenOff: generate_offsets(collision_offsets, COLLISION_RANGE=5)
    GenOff->>CollArr: Fill array with 5x5 grid (-2 to 2)
    GenOff-->>Init: Array populated
    
    Init->>GenOff: generate_offsets(z_offsets, Z_RANGE=15)
    GenOff->>ZArr: Fill array with 15x15 grid (-7 to 7)
    GenOff-->>Init: Array populated
    
    Note over Compute,Step: Runtime Usage
    Compute->>CheckN: checkNeighbors(x, y, collision_offsets, 25)
    CheckN->>CollArr: Read offsets for collision detection
    CheckN-->>Compute: Return nearby entities
    
    Step->>CheckN: checkNeighbors(x, y, z_offsets, 225)
    CheckN->>ZArr: Read offsets for z-coordinate calculation
    CheckN-->>Step: Return nearby road segments
Loading

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

Additional Comments (1)

  1. pufferlib/ocean/drive/drive.h, line 1736 (link)

    style: Use Z_RANGE * Z_RANGE constant instead of hardcoded 225 for consistency with line 1177.

    Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

1 file reviewed, 3 comments

Edit Code Review Agent Settings | Greptile

Comment on lines 138 to 139
int collision_offsets[25][2] = {0};
int z_offsets[225][2] = {0};
Copy link

Choose a reason for hiding this comment

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

logic: Non-static global variables in header will cause multiple definition linker errors. Since drive.h is included by binding.c, drive.c (via drivenet.h), and visualize.c (via drivenet.h), these variables will be defined in multiple compilation units.

Suggested change
int collision_offsets[25][2] = {0};
int z_offsets[225][2] = {0};
// Offset arrays (defined in drive.c)
extern int collision_offsets[25][2];
extern int z_offsets[225][2];

Then add the actual definitions in a .c file:

// In drive.c or a new offsets.c file
int collision_offsets[25][2] = {0};
int z_offsets[225][2] = {0};
Prompt To Fix With AI
This is a comment left during a code review.
Path: pufferlib/ocean/drive/drive.h
Line: 138:139

Comment:
**logic:** Non-static global variables in header will cause multiple definition linker errors. Since `drive.h` is included by `binding.c`, `drive.c` (via `drivenet.h`), and `visualize.c` (via `drivenet.h`), these variables will be defined in multiple compilation units.

```suggestion
// Offset arrays (defined in drive.c)
extern int collision_offsets[25][2];
extern int z_offsets[225][2];
```

Then add the actual definitions in a `.c` file:
```c
// In drive.c or a new offsets.c file
int collision_offsets[25][2] = {0};
int z_offsets[225][2] = {0};
```

How can I resolve this? If you propose a fix, please make it concise.

Copy link
Author

Choose a reason for hiding this comment

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

fixed

Comment on lines 123 to 135
void generate_offsets(int offsets[][2], int offset_range) {
int half_grid = offset_range / 2;
int left_most = -half_grid;
int right_most = offset_range + left_most - 1;
int index = 0;
for (int dy = left_most; dy <= right_most; dy++) {
for (int dx = left_most; dx <= right_most; dx++) {
offsets[index][0] = dx;
offsets[index][1] = dy;
index++;
}
}
}
Copy link

Choose a reason for hiding this comment

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

logic: Function definition in header will cause multiple definition linker errors when included in multiple compilation units. Should be either static inline or declared here and defined in a .c file.

Suggested change
void generate_offsets(int offsets[][2], int offset_range) {
int half_grid = offset_range / 2;
int left_most = -half_grid;
int right_most = offset_range + left_most - 1;
int index = 0;
for (int dy = left_most; dy <= right_most; dy++) {
for (int dx = left_most; dx <= right_most; dx++) {
offsets[index][0] = dx;
offsets[index][1] = dy;
index++;
}
}
}
// Function declaration (definition should be in drive.c)
void generate_offsets(int offsets[][2], int offset_range);

Or use static inline:

Suggested change
void generate_offsets(int offsets[][2], int offset_range) {
int half_grid = offset_range / 2;
int left_most = -half_grid;
int right_most = offset_range + left_most - 1;
int index = 0;
for (int dy = left_most; dy <= right_most; dy++) {
for (int dx = left_most; dx <= right_most; dx++) {
offsets[index][0] = dx;
offsets[index][1] = dy;
index++;
}
}
}
static inline void generate_offsets(int offsets[][2], int offset_range) {
int half_grid = offset_range / 2;
int left_most = -half_grid;
int right_most = offset_range + left_most - 1;
int index = 0;
for (int dy = left_most; dy <= right_most; dy++) {
for (int dx = left_most; dx <= right_most; dx++) {
offsets[index][0] = dx;
offsets[index][1] = dy;
index++;
}
}
}
Prompt To Fix With AI
This is a comment left during a code review.
Path: pufferlib/ocean/drive/drive.h
Line: 123:135

Comment:
**logic:** Function definition in header will cause multiple definition linker errors when included in multiple compilation units. Should be either `static inline` or declared here and defined in a `.c` file.

```suggestion
// Function declaration (definition should be in drive.c)
void generate_offsets(int offsets[][2], int offset_range);
```

Or use `static inline`:
```suggestion
static inline void generate_offsets(int offsets[][2], int offset_range) {
    int half_grid = offset_range / 2;
    int left_most = -half_grid;
    int right_most = offset_range + left_most - 1;
    int index = 0;
    for (int dy = left_most; dy <= right_most; dy++) {
        for (int dx = left_most; dx <= right_most; dx++) {
            offsets[index][0] = dx;
            offsets[index][1] = dy;
            index++;
        }
    }
}
```

How can I resolve this? If you propose a fix, please make it concise.

Copy link
Author

Choose a reason for hiding this comment

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

fixed

@mpragnay mpragnay merged commit 1b67cfc into aditya/z_axis_PR Jan 18, 2026
10 checks passed
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