Skip to content

Refactor Hardcoded Values in Enemy SQL #3

Description

@patricktrainer

The recently added enemy logic (#2) within the tickEnemies function relies on a SQL query that contains several hardcoded numerical values to determine behavior.

Location: index.html > tickEnemies function > SQL UPDATE query with CTEs.

Current Hardcoded Values:

  • Distance Thresholds (Squared): 9 (retreat), 25 (circle/approach boundary), 81 (activation range)
  • Base Movement Step: 0.1
  • Movement Randomization Factors: 0.9, 0.2 (used as 0.9 + random() * 0.2)
  • Circle Direction Change Probability: 0.02 (used as random() < 0.02)

Problem:

Embedding these "magic numbers" directly in the SQL string makes it:

  1. Harder to Tune: Adjusting enemy behavior requires finding and modifying values within a complex SQL query.
  2. Less Readable: It's not immediately obvious what these numbers represent without carefully reading the surrounding SQL logic.
  3. Error-Prone: Modifying the SQL string for tuning increases the risk of introducing syntax errors.

Proposed Solutions:

Consider extracting these values out of the SQL string itself:

  1. JavaScript Constants: Define these parameters as const variables at the top of the relevant JavaScript scope. Then, use template literals or string concatenation to insert these variables into the SQL query string before execution.
    • Pros: Relatively simple, keeps configuration within the code.
    • Cons: Still requires a code change and redeployment to adjust values.
  2. settings Table: Add new columns/rows to the existing settings SQL table to store these AI parameters (e.g., enemy_retreat_dist_sq, enemy_move_step, enemy_circle_change_prob). The tickEnemies function would need to query these settings first and then use them in the main AI movement query.
    • Pros: More data-driven, potentially allows for easier changes if the settings were loaded dynamically (though maybe overkill here). Keeps game parameters consolidated.
    • Cons: Adds a small overhead (querying settings first), slightly more complex implementation.

Goal:

Refactoring these values will improve the maintainability, readability, and ease of tuning for the enemy behavior.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions